无法从EF数据库中检索数据

时间:2015-08-23 18:25:08

标签: c# asp.net-mvc entity-framework

我有一个简单的应用程序,它应该显示列表的元素,但它在调试时返回一个空数组。我需要这些元素以动态显示。我想知道程序是否因为以下代码而无法正常工作......

StoreController.cs

using GetMed.Models;

namespace GetMed.Controllers
{
    public class StoreController : Controller
    {
        GetMedEntities storeDB = new GetMedEntities();
        //
        // GET: /Store/
        public ActionResult Index()
        {
            var categories = storeDB.Categories.ToList();

            return View(categories);
        }
    }
}

SampleData.cs:

using System.Data.Entity;

namespace GetMed.Models
{
    public class SampleData : DropCreateDatabaseIfModelChanges<GetMedEntities>
    {
        protected override void Seed(GetMedEntities context)
        {
            var categories = new List<Category>
            {
                new Category { Name = "Infections" },
                new Category { Name = "Antibiotics" },
                new Category { Name = "Vitamins" },
                new Category { Name = "Cosmetics" }
            };
        }
    }
}

GetMedEntities.cs

namespace GetMed.Models
{
    public class GetMedEntities : DbContext
    {
        public DbSet<Category> Categories { get; set; }
    }
}

Index.cs:

@model IEnumerable<GetMed.Models.Category>

@{
    ViewBag.Title = "Index";
}

<h3>Browse Categories</h3>
<p>Select from @Model.Count() categories:</p>
<ul>
    @foreach (var category in Model)
    {
        <li>@Html.ActionLink(category.Name, "Browse", new { category = category.Name })</li>
    }
</ul>

2 个答案:

答案 0 :(得分:2)

如果查看数据库,您会发现其中没有数据。看看你的Seed方法:你期望它做什么?

创建数据后,您还需要执行两个步骤:

  1. 将其添加到DbContext
  2. 跟踪的集合中
  3. 将其保存到数据库
  4. 这导致了这些额外的行:

    context.Categories.AddRange(categories);
    context.SaveChanges();
    

答案 1 :(得分:1)

  1. 永远不要初始化EF背景,一旦它会打击你,相信我
  2. 通过EF向DB添加记录时,您必须致电

    using (var context = new GetMedEntities)
    {
        // YOUR ADD ROUTINE GOES HERE
    
        context.SaveChanges();
    }
    
  3. <强>更新

    更改班级SampleData

        public class SampleData : DropCreateDatabaseIfModelChanges<GetMedEntities>
        {
            protected override void Seed(GetMedEntities context)
            {
    //I STRICTLY RECOMMEND NOT TO PROVIDE CONTEXT AS A METHOD PARAMETER 
    //YOU HAVE TO WRITE PROPER DB LAYER TO DO SO
    
                context.Categories.AddRange( new List<Category>()
                {
                    new Category { Name = "Infections" },
                    new Category { Name = "Antibiotics" },
                    new Category { Name = "Vitamins" },
                    new Category { Name = "Cosmetics" }
                });
                context.SaveChanges();
            }
        }
    

    但简单的解决方案如下:

    public class SampleData 
    {
      public void SeedSampleData() 
      {
        var context = new GedMedEntities();
        context.Categories.AddRange( new List<Category>()
        {
          new Category { Name = "Infections" },
          new Category { Name = "Antibiotics" },
          new Category { Name = "Vitamins" },
          new Category { Name = "Cosmetics" }
        });
        context.SaveChanges();