コード例 #1
0
        /// <summary>
        /// 设置数据库初始化,策略为自动迁移到最新版本
        /// </summary>
        public static void Initialize()
        {
            CodeFirstDbContext context = new CodeFirstDbContext();
            IDatabaseInitializer <CodeFirstDbContext> initializer;

            if (!context.Database.Exists())
            {
                initializer = new CreateDatabaseIfNotExistsWithSeed();
            }
            else
            {
                initializer = new NullDatabaseInitializer <CodeFirstDbContext>();

                // initializer = new MigrateDatabaseToLatestVersion<CodeFirstDbContext, MigrationsConfiguration>();
            }

            Database.SetInitializer(initializer);

            //EF预热,解决EF6第一次加载慢的问题
            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
            StorageMappingItemCollection mappingItemCollection = (StorageMappingItemCollection)objectContext.ObjectStateManager
                                                                 .MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);

            mappingItemCollection.GenerateViews(new List <EdmSchemaError>());
            context.Dispose();
        }
コード例 #2
0
        public static ApplicationDbContext Create()
        {
            IDatabaseInitializer <ApplicationDbContext> strategy;

            switch (ConfigurationManager.AppSettings["Database_Initialize_Strategy"])
            {
            case "CreateDatabaseIfNotExists":
                strategy = new CreateDatabaseIfNotExists <ApplicationDbContext>();
                break;

            case "DropCreateDatabaseAlways":
                strategy = new DropCreateDatabaseAlways <ApplicationDbContext>();
                break;

            case "DropCreateDatabaseIfModelChanges":
                strategy = new DropCreateDatabaseIfModelChanges <ApplicationDbContext>();
                break;

            case "MigrateDatabaseToLatestVersion":
                strategy = new MigrateDatabaseToLatestVersion <ApplicationDbContext, Configuration>();
                break;

            default:
                strategy = new NullDatabaseInitializer <ApplicationDbContext>();
                break;
            }
            Database.SetInitializer(strategy);
            return(new ApplicationDbContext());
        }
コード例 #3
0
        public DefaultDbConfiguration()
        {
            IDatabaseInitializer<DefaultDbContext> databaseInitializer;

            if (GlobalConfig.AutomaticMigrationsEnabled)
            {
                databaseInitializer = new MigrateDatabaseToLatestVersion<DefaultDbContext, Configuration>();
                SetContextFactory(typeof(DefaultDbContext), () =>
                {
                    var work = new HttpContextWrapper(HttpContext.Current).Request.RequestContext.GetWorkContext();
                    return work.Resolve<EntityFrameworkDbContextFactory>().Create();
                });
            }
            else
                databaseInitializer = new NullDatabaseInitializer<DefaultDbContext>();

            SetDatabaseInitializer(databaseInitializer);
        }
コード例 #4
0
        public DefaultDbConfiguration()
        {
            IDatabaseInitializer <DefaultDbContext> databaseInitializer;

            if (GlobalConfig.AutomaticMigrationsEnabled)
            {
                databaseInitializer = new MigrateDatabaseToLatestVersion <DefaultDbContext, Configuration>();
                SetContextFactory(typeof(DefaultDbContext), () =>
                {
                    var work = new HttpContextWrapper(HttpContext.Current).Request.RequestContext.GetWorkContext();
                    return(work.Resolve <EntityFrameworkDbContextFactory>().Create());
                });
            }
            else
            {
                databaseInitializer = new NullDatabaseInitializer <DefaultDbContext>();
            }

            SetDatabaseInitializer(databaseInitializer);
        }
コード例 #5
0
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            IDatabaseInitializer <MySqlDbContext> initializer;

            if (!Database.Exists(_nameOrConnectionString))
            {
                //初始化代码放在CreateDatabaseIfNotExists中
                //initializer = new CreateDatabaseIfNotExists<MySqlDbContext>();
                initializer = new CreateDatabaseIfNotExists <MySqlDbContext>();
            }
            else
            {
                //初始化代码不要放在MigrateDatabaseToLatestVersion中
                //initializer = new MigrateDatabaseToLatestVersion<MySqlDbContext, MigrationConfiguration>();

                //MySql的数据库迁移有bug,不能用
                ////相当于null,不进行初始化
                initializer = new NullDatabaseInitializer <MySqlDbContext>();
            }

            // The database initializer is called when a the given System.Data.Entity.DbContext type is used to access a database for the first time.
            //因为第一次访问数据库时调用Seed来初始化,所以目前检查数据库是否存在并没有调用Seed
            Database.SetInitializer(initializer);
        }