【问题标题】:Entity Framework Code First Seeding Data实体框架代码优先播种数据
【发布时间】:2016-07-20 01:38:39
【问题描述】:

我在实体框架中使用代码优先方法,但我无法将默认数据播种到表中。请帮忙。

型号

public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public int Salary { get; set; }


        public virtual Department Departments { get; set; }

    }
 public class Department
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }

        public Department()
        {
            this.Employees = new List<Employee>();
        }

    }

初始化器

public class DepartmentInitializer : DropCreateDatabaseIfModelChanges<EmployeeDBContext>
    {
        protected override void Seed(EmployeeDBContext context)
        {
            IList<Department> lst = new List<Department>
            {
                new Department
                {
                    Name = "Developer",
                    Location = "Bangalore"
                },
                new Department
                {
                    Name = "Tester",
                    Location = "Bangalore"
                },
                new Department
                {
                    Name = "IT Services",
                    Location = "Chennai"
                }
            };
            foreach (var item in lst)
            {
                context.Departments.Add(item);
            }
            context.SaveChanges();
        }
    }

主应用

class Program
    {
        static void Main(string[] args)
        {
            using (var db = new EmployeeDBContext())
            {
                Database.SetInitializer<EmployeeDBContext>(new DepartmentInitializer());
            }
        }
    }

【问题讨论】:

  • 您使用的是哪个版本的 EF?播种随着不同的版本而改变。

标签: c# entity-framework console-application


【解决方案1】:

对于 Entity Framework 版本 6,使用“迁移”是对数据库进行版本控制的首选方法,使用本教程中所示的“Configuration.Seed”方法:

http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-3

您是否尝试过从包管理器控制台运行“更新数据库”以使其工作?

我知道在 EF6 中使用较旧的播种方法时遇到了问题。 Entity Framework Core 1(以前的 EF7)的迁移也发生了变化,因此请确保您将正确的技术应用于正确的版本。

【讨论】:

  • 我尝试了同样的方法并且它正在工作,但为什么上述方法在 EF 6 中不起作用。有什么想法吗?
  • 我认为这是从 DatabaseContext 继承类没有正确调用种子方法。见:stackoverflow.com/questions/25524238/…
【解决方案2】:

尝试实际查询您的数据库

在我的机器上,当我第一次查询播种机时,它就会运行。

using (var db = new EmployeeDBContext())
{
    Database.SetInitializer<EmployeeDBContext>(new DepartmentInitializer());
    var depts = db.Departments.ToList();
}

【讨论】:

  • @S.Sagar 好吧,如果您的数据库已经存在,那么它不会重新植入它。 DropCreateDatabaseIfModelChanges 仅在数据库模型更改或删除数据库时重新播种。
  • 我更改了模型并在 Employee Class 中添加了一个标量属性。但仍然没有成功。
  • @S.Sagar 发生了什么事?是在扔DoesNotWorkException吗?
  • 现在我删除了我的数据库,但它仍然显示,部门已经存在
猜你喜欢
  • 1970-01-01
  • 2015-02-18
  • 2013-09-16
  • 1970-01-01
  • 2017-09-01
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
相关资源
最近更新 更多