Ejemplo 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)
            {
                if (!_migrationsChecker.IsMigrationsConfigured(internalContext, () => false))
                {
                    var migrator = createMigrator(
                        GetMigrationsConfiguration(internalContext),
                        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();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Runs the the registered <see cref="IDatabaseInitializer{TContext}" /> on this context.
 /// If "force" is set to true, then the initializer is run regardless of whether or not it
 /// has been run before.  This can be useful if a database is deleted while an app is running
 /// and needs to be reinitialized.
 /// If "force" is set to false, then the initializer is only run if it has not already been
 /// run for this context, model, and connection in this app domain. This method is typically
 /// used when it is necessary to ensure that the database has been created and seeded
 /// before starting some operation where doing so lazily will cause issues, such as when the
 /// operation is part of a transaction.
 /// </summary>
 /// <param name="force">
 /// If set to <c>true</c> the initializer is run even if it has already been run.
 /// </param>
 public void Initialize(bool force)
 {
     if (force)
     {
         _internalContext.MarkDatabaseInitialized();
         _internalContext.PerformDatabaseInitialization();
     }
     else
     {
         _internalContext.Initialize();
     }
 }
Ejemplo 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)
        {
            //Contract.Requires(internalContext != null);
            //Contract.Requires(createMigrator != null);
            // objectContext may be null when testing.

            var sqlGenerator = _resolver.Value.GetService<MigrationSqlGenerator>(internalContext.ProviderName);

            if (internalContext.CodeFirstModel != null
                && sqlGenerator != null)
            {
                var contextType = internalContext.Owner.GetType();

                var migrator = createMigrator(
                    new DbMigrationsConfiguration
                        {
                            ContextType = contextType,
                            AutomaticMigrationsEnabled = true,
                            MigrationsAssembly = contextType.Assembly,
                            MigrationsNamespace = contextType.Namespace,
                            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();
        }