Asp.net mvc 4 无法先在EF5代码中为数据设定种子
我在MVC4项目中使用EF5。我的问题是我无法让种子数据工作。我在发帖前搜索了这个问题,但我想不出来。所以请给我指引。谢谢 以下是我的DbContext类:Asp.net mvc 4 无法先在EF5代码中为数据设定种子,asp.net-mvc-4,entity-framework-5,Asp.net Mvc 4,Entity Framework 5,我在MVC4项目中使用EF5。我的问题是我无法让种子数据工作。我在发帖前搜索了这个问题,但我想不出来。所以请给我指引。谢谢 以下是我的DbContext类: public class AminBarEntities : DbContext { public DbSet<Branch> Branches { get; set; } public DbSet<CarType> CarTypes { get; set; } } public class Ami
public class AminBarEntities : DbContext
{
public DbSet<Branch> Branches { get; set; }
public DbSet<CarType> CarTypes { get; set; }
}
public class AminBarEntitiesInitializer :DropCreateDatabaseAlways<AminBarEntities>
{
protected override void Seed(AminBarEntities dbContext)
{
// seed data
dbContext.Branches.Add(new Branch() { BranchName = "قزوین" });
dbContext.Branches.Add(new Branch() { BranchName = "البرز" });
dbContext.CarTypes.Add(new CarType() { CarTypeName = "موتور" });
dbContext.CarTypes.Add(new CarType() { CarTypeName = "وانت" });
dbContext.CarTypes.Add(new CarType() { CarTypeName = "نیسان" });
dbContext.CarTypes.Add(new CarType() { CarTypeName = "مزدا" });
dbContext.CarTypes.Add(new CarType() { CarTypeName = "مینی خاور" });
dbContext.CarTypes.Add(new CarType() { CarTypeName = "خاور 4 متری" });
dbContext.CarTypes.Add(new CarType() { CarTypeName = "خاور 6 متری" });
// dbContext.SaveChanges();
// base.Seed(dbContext);
}
}
但是没有成功 什么不适用于此代码?它会出错吗?为什么您的
SaveChanges()
被注释掉了?“不成功”并不能解释你的问题是什么。我设置了一个断点,但我认为它根本没有提出这个初始值设定项。它不会抛出任何错误。没有成功意味着它不会将数据植入我的数据库。所以我的问题是我哪里做错了?为什么dbContext.SaveChanges()代码>有注释吗?以前没有注释过,我在某个地方读到没有必要使用那行代码!!不管怎样,当它没有被注释掉的时候,它就不起作用了。
System.Data.Entity.Database.SetInitializer(new AminBar.Models.AminBarEntitiesInitializer());