Exemplo n.º 1
0
        // <summary>
        // Creates a database using the core provider (i.e. ObjectContext.CreateDatabase) or
        // by using Code First Migrations <see cref="DbMigrator" /> to create an empty database
        // and the perform an automatic migration to the current model.
        // </summary>
        public virtual void CreateDatabase(
            InternalContext internalContext,
            Func <DbMigrationsConfiguration, DbContext, MigratorBase> createMigrator,
            ObjectContext objectContext)
        {
            DebugCheck.NotNull(internalContext);
            DebugCheck.NotNull(createMigrator);
            // objectContext may be null when testing.

            if (internalContext.CodeFirstModel != null &&
                _resolver.GetService <Func <MigrationSqlGenerator> >(internalContext.ProviderName) != null)
            {
                createMigrator(
                    internalContext.MigrationsConfiguration,
                    internalContext.Owner).Update();
            }
            else
            {
                internalContext.DatabaseOperations.Create(objectContext);
                internalContext.SaveMetadataToDatabase();
            }

            // If the database is created explicitly, then this is treated as overriding the
            // database initialization strategy, so make it as already run.
            internalContext.MarkDatabaseInitialized();
        }
Exemplo n.º 2
0
 public virtual void CreateDatabase(
     InternalContext internalContext,
     Func <DbMigrationsConfiguration, DbContext, MigratorBase> createMigrator,
     ObjectContext objectContext)
 {
     if (internalContext.CodeFirstModel != null && this._resolver.GetService <Func <MigrationSqlGenerator> >((object)internalContext.ProviderName) != null)
     {
         createMigrator(internalContext.MigrationsConfiguration, internalContext.Owner).Update();
     }
     else
     {
         internalContext.DatabaseOperations.Create(objectContext);
         internalContext.SaveMetadataToDatabase();
     }
     internalContext.MarkDatabaseInitialized();
 }
Exemplo n.º 3
0
        /// <summary>
        ///     Creates a database using the core provider (i.e. ObjectContext.CreateDatabase) or
        ///     by using Code First Migrations <see cref="DbMigrator" /> to create an empty database
        ///     and the perform an automatic migration to the current model.
        ///     Migrations is used if Code First is being used and the EF provider is for SQL Server
        ///     or SQL Compact. The core is used for non-Code First models and for other providers even
        ///     when using Code First.
        /// </summary>
        public virtual void CreateDatabase(
            InternalContext internalContext,
            Func <DbMigrationsConfiguration, DbContext, MigratorBase> createMigrator,
            ObjectContext objectContext)
        {
            DebugCheck.NotNull(internalContext);
            DebugCheck.NotNull(createMigrator);
            // objectContext may be null when testing.

            if (internalContext.CodeFirstModel != null &&
                _resolver.GetService <MigrationSqlGenerator>(internalContext.ProviderName) != null)
            {
                var contextType = internalContext.Owner.GetType();

                var migrator = createMigrator(
                    new DbMigrationsConfiguration
                {
                    ContextType = contextType,
                    AutomaticMigrationsEnabled = true,
                    MigrationsAssembly         = contextType.Assembly,
                    MigrationsNamespace        = contextType.Namespace,
                    ContextKey     = internalContext.ContextKey,
                    TargetDatabase =
                        new DbConnectionInfo(
                            internalContext.OriginalConnectionString, internalContext.ProviderName)
                },
                    internalContext.Owner);

                migrator.Update();
            }
            else
            {
                internalContext.DatabaseOperations.Create(objectContext);
                internalContext.SaveMetadataToDatabase();
            }

            // If the database is created explicitly, then this is treated as overriding the
            // database initialization strategy, so make it as already run.
            internalContext.MarkDatabaseInitialized();
        }