实体框架种子不能正常工作 [英] Entity Framework Seed not working

查看:121
本文介绍了实体框架种子不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个在我的web.config。

I have this on my web.config.

<add name="AppDataContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\AppDatabase.mdf;Initial Catalog=AppDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />

Global.asax中应用程序启动

Global.asax APplication Start

 Database.SetInitializer<AppDataContext>(new AppDataContextInitializer());

AppDataContextInitializer.cs

AppDataContextInitializer.cs

public class AppDataContextInitializer : System.Data.Entity.CreateDatabaseIfNotExists<AppDataContext>
    {
        protected override void Seed(AppDataContext context)
        {
            #region Seed Modules
            context.ModuleList.Add(new Module() { Id = 1, ModuleName = "Contabilidad", FontAwesomeClass = "ambulance" });
            context.ModuleList.Add(new Module() { Id = 2, ModuleName = "Recursos Humanos", FontAwesomeClass = "heartbeat" });
            context.ModuleList.Add(new Module() { Id = 3, ModuleName = "Inventario", FontAwesomeClass = "medkit" }); 
            #endregion


            base.Seed(context);
        }
    }

AppDataContext.cs

AppDataContext.cs

public class AppDataContext : DbContext
    {

        public AppDataContext(): base("AppDataContext")
        {
        }

        public DbSet<Module> ModuleList { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

和module.cs

and the module.cs

 public class Module
    {
        [Key]
        public int Id { get; set; }
        public string ModuleName { get; set; }
        public string FontAwesomeClass { get; set; }
    }

但不会创建我的数据库,我错过什么?

However my database is not created, what am I missing?

更新1,我加了这对在Global.asax

Update 1, I added this on the global.asax

 AppInitializacionHandler.Initialize();

我创建这个类:

public class AppInitializacionHandler
    {
        public static void Initialize()
        {
            Database.SetInitializer(new AppDataContextInitializer()); //if u want to use your initializer
            using (var db = new AppDataContext())
            {
                db.Database.Initialize(true);
            }
        }
    }

和初始化种子和以前一样

and the initializer seed is as before

创建数据库,但没有添加的行。

Database is created but no ROWS are added.

推荐答案

初始值是从来没有所谓,如果你不访问数据库。如果你想创建数据库时,应用程序启动调用初始化方法在Global.asax中:

The Initializer is never called if you don't access to the DB. If you want to create the DB when the application start call the Initialize method in your Global.asax:

context.Database.Initialize(true)();

更新1:

问题是你要设置初始化在不同的数据库实例,试试这个:

using (var db = new AppDataContext())
{
   db.Database.SetInitializer(new AppDataContextInitializer());
   db.Database.Initialize(true);
}

或者设置初始化器上的的DbContext 构造器:

 public AppDataContext()
 {
    Database.SetInitializer(new AppDataContextInitializer());
 }

这篇关于实体框架种子不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆