我将此代码放在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>());
}
答案 0 :(得分:2)
为您的上下文类试试这个:
public ApplicationDbContext()
: base("DefaultConnection")
{
Database.SetInitializer(new BcoInitializer());
}