Entity framework 从未调用过种子方法
Entity framework seed method never invoked
我把这段代码放在 global.asax
文件中:
System.Data.Entity.Database.SetInitializer(new BcoInitializer());
ApplicationDbContext c = new ApplicationDbContext();
c.Database.Initialize(true);
var x = c.ProductCategories.ToList();
这将重新创建我的数据库。
但是 Seed()
从未被调用过,我不知道为什么?
public class BcoInitializer : System.Data.Entity.DropCreateDatabaseAlways<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{
base.Seed(context);
var currencies = new List<Currency>
{
new Currency{CurrencyId="USD", Symbol="$"}
};
这是我的上下文:
public ApplicationDbContext()
: base("DefaultConnection")
{
Database.SetInitializer(new CreateDatabaseIfNotExists<ApplicationDbContext>());
}
根据您的情况试试这个 class:
public ApplicationDbContext()
: base("DefaultConnection")
{
Database.SetInitializer(new BcoInitializer());
}
我把这段代码放在 global.asax
文件中:
System.Data.Entity.Database.SetInitializer(new BcoInitializer());
ApplicationDbContext c = new ApplicationDbContext();
c.Database.Initialize(true);
var x = c.ProductCategories.ToList();
这将重新创建我的数据库。
但是 Seed()
从未被调用过,我不知道为什么?
public class BcoInitializer : System.Data.Entity.DropCreateDatabaseAlways<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{
base.Seed(context);
var currencies = new List<Currency>
{
new Currency{CurrencyId="USD", Symbol="$"}
};
这是我的上下文:
public ApplicationDbContext()
: base("DefaultConnection")
{
Database.SetInitializer(new CreateDatabaseIfNotExists<ApplicationDbContext>());
}
根据您的情况试试这个 class:
public ApplicationDbContext()
: base("DefaultConnection")
{
Database.SetInitializer(new BcoInitializer());
}