实体框架种子方法从未被调用过

时间:2015-01-03 13:21:24

标签: entity-framework ef-code-first entity-framework-6

我将此代码放在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>());
        }

1 个答案:

答案 0 :(得分:2)

为您的上下文类试试这个:

 public ApplicationDbContext()
        : base("DefaultConnection")
    {   
        Database.SetInitializer(new BcoInitializer());
    }