简体   繁体   English

无法从EF数据库检索数据

[英]Can't retrieve data from EF database

I have a simple application which should display the elements of a list, but it returns an empty array when debugging. 我有一个简单的应用程序,它应该显示列表的元素,但是在调试时它会返回一个空数组。 I need this elements to be displayed dinamically. 我需要将此元素进行动态显示。 I'm wondering if the program is not working because of the following code... 我想知道程序是否由于以下代码而无法正常工作...

StoreController.cs 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: 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 GetMedEntities.cs

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

Index.cs: 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>

If you look at your database, you will notice that there is no data in it. 如果查看数据库,您会发现其中没有数据。 Look at your Seed method: what do you expect it to do? 查看您的Seed方法:您希望它做什么?

After creating your data you have two more steps to do: 创建数据后,您还需要执行两个步骤:

  1. Adding it to a collection tracked by your DbContext 将其添加到您的DbContext跟踪的DbContext
  2. Saving it to the database 将其保存到数据库

This results in these extra lines: 这导致这些额外的行:

context.Categories.AddRange(categories);
context.SaveChanges();
  1. Never initialize EF context gloablly, once it gonna hit you hard, believe me 永远不要过分初始化EF上下文,一旦它给您带来沉重打击,请相信我
  2. When adding records to DB via EF you have to call 通过EF将记录添加到数据库时,您必须调用

     using (var context = new GetMedEntities) { // YOUR ADD ROUTINE GOES HERE context.SaveChanges(); } 

UPDATE 更新

Change your class SampleData 更改类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();
        }
    }

But the simple solution is the following: 但是,简单的解决方案如下:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM