我正在开发一个.Net项目。我使用实体框架代码第一种方法与数据库进行交互。我在开发过程中将一些模拟数据播种到我的数据库中。但播种不起作用。我点了这个链接 - http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx。
这是我的ContextInitializer类
public class ContextInitializer : System.Data.Entity.CreateDatabaseIfNotExists<StoreContext>
{
protected override void Seed(StoreContext context)
{
IList<Brand> brands = new List<Brand>();
brands.Add(new Brand { Name = "Giordano" ,TotalSale = 1 });
brands.Add(new Brand { Name = "Nike" , TotalSale = 3 });
foreach(Brand brand in brands)
{
context.Brands.Add(brand);
}
base.Seed(context);
context.SaveChanges();
}
}
这是我的上下文类
public class StoreContext : DbContext,IDisposable
{
public StoreContext():base("DefaultConnection")
{
Database.SetInitializer(new ContextInitializer());
}
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Brand> Brands { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
这是我的品牌类
public class Brand
{
public int Id { get; set; }
[Required]
[MaxLength(40)]
public string Name { get; set; }
public int TotalSale { get; set; }
}
我在线搜索解决方案,然后按照说明操作。我也运行了context.SaveChanges。但它没有将数据播种到数据库。为什么它不起作用?
答案 0 :(得分:3)
您使用了错误的初始化程序,只有在数据库不存在时才会调用CreateDatabaseIfNotExists!
您可以使用DropCreateDatabaseIfModelChanges:
解决方案1)
<script type="text/javascript">
function limit_input() {
var field = document.getElementById("number_field");
var max_length = 3;
if (field.value.length > max_length) {
field.value = field.value.slice(0, max_length);
}
}
</script>
你必须小心这种方法, !!!删除!!! 所有现有数据。
解决方案2)
创建自定义DbMigrationsConfiguration:
public class ContextInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<StoreContext>
{
通过这种方式你可以调用(!!在创建DbContext之前或者在DbContext构造函数中!!):
public class Configuration : DbMigrationsConfiguration<StoreContext>
{
public Configuration()
{
// Take here! read about this property!
this.AutomaticMigrationDataLossAllowed = true;
this.AutomaticMigrationsEnabled = false;
}
protected override void Seed(StoreContext context)
{
IList<Brand> brands = new List<Brand>();
brands.Add(new Brand { Name = "Giordano", TotalSale = 1 });
brands.Add(new Brand { Name = "Nike", TotalSale = 3 });
foreach (Brand brand in brands)
{
context.Brands.AddOrUpdate(m => m.Name, brand);
}
base.Seed(context);
context.SaveChanges();
}
}
注意:
如果你在我的例子中保留默认值,那么你不必改变任何东西!
答案 1 :(得分:0)
设置数据库的初始化程序必须在之前创建上下文...
public StoreContext():base("DefaultConnection")
{
Database.SetInitializer(new ContextInitializer());
}
要迟到了。如果你把它设为静态,那么它可以工作:
static StoreContext()
{
Database.SetInitializer(new ContextInitializer());
}
答案 2 :(得分:0)
如果删除现有数据库并且EF将创建和播种数据
,则代码正常工作或强>
您可以DbMigrationsConfiguration
CreateDatabaseIfNotExists
使用public class ContextInitializer : System.Data.Entity.Migrations.DbMigrationsConfiguration<StoreContext>
{
public ContextInitializer()
{
this.AutomaticMigrationDataLossAllowed = true;
this.AutomaticMigrationsEnabled = true;
}
protected override void Seed(StoreContext context)
{
IList<Brand> brands = new List<Brand>();
brands.Add(new Brand { Name = "Giordano", TotalSale = 1 });
brands.Add(new Brand { Name = "Nike", TotalSale = 3 });
foreach (Brand brand in brands)
{
context.Brands.AddOrUpdate(m => m.Name, brand);
}
base.Seed(context);
context.SaveChanges();
}
}
并更改您的代码,如下所示:
首先,您必须删除现有数据库
ContextInitializer类
public class StoreContext : DbContext, IDisposable
{
public StoreContext() : base("DefaultConnection")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<StoreContext, ContextInitializer>());
// Database.SetInitializer(new ContextInitializer());
}
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Brand> Brands { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
StoreContext
var year = 2017; //get current year dynamically
然后种子中的任何更改都会自动反映到您的数据库中