EntityFramework-CodeFirst数据上下文
Inheritance: DbContext, IUnitOfWork, IDependency
Example #1
0
        //public static readonly ICollection<Assembly> MapperAssemblies = new List<Assembly>();
        /// <summary>
        ///     设置数据库初始化,策略为自动迁移到最新版本
        /// </summary>
        public static void Initialize()
        {
            var context = new ChiakiYuDbContext();
            IDatabaseInitializer<ChiakiYuDbContext> initializer;
            if (!context.Database.Exists())
            {
                initializer = new CreateDatabaseIfNotExistsWithSeed();
            }
            else
            {
                initializer = new MigrateDatabaseToLatestVersion<ChiakiYuDbContext, Configuration>();
            }
            Database.SetInitializer(initializer);

            //EF预热,解决EF6第一次加载慢的问题
            var objectContext = ((IObjectContextAdapter) context).ObjectContext;
            var mappingItemCollection = (StorageMappingItemCollection) objectContext.ObjectStateManager
                .MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
            mappingItemCollection.GenerateViews(new List<EdmSchemaError>());
            context.Dispose();
        }
        //public static readonly ICollection<Assembly> MapperAssemblies = new List<Assembly>();

        /// <summary>
        ///     设置数据库初始化,策略为自动迁移到最新版本
        /// </summary>
        public static void Initialize()
        {
            var context = new ChiakiYuDbContext();
            IDatabaseInitializer <ChiakiYuDbContext> initializer;

            if (!context.Database.Exists())
            {
                initializer = new CreateDatabaseIfNotExistsWithSeed();
            }
            else
            {
                initializer = new MigrateDatabaseToLatestVersion <ChiakiYuDbContext, Configuration>();
            }
            Database.SetInitializer(initializer);

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

            mappingItemCollection.GenerateViews(new List <EdmSchemaError>());
            context.Dispose();
        }