Example #1
0
    public void InitializeDatabase(ApplicationDbContext context)
    {
        var exists = new DatabaseTableChecker().AnyModelTableExists(context.InternalContext);

        if (exists == DatabaseExistenceState.Exists)
        {
            // maybe check if certain data exists and call the `Seed` method if
            // it doesn't
            return;
        }
        // Throw some error if it doesn't exist
    }
Example #2
0
        /// <summary>
        /// Executes the strategy to initialize the database for the given context.
        /// </summary>
        /// <param name="context"> The context. </param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="context" />
        /// is
        /// <c>null</c>
        /// .
        /// </exception>
        public virtual void InitializeDatabase(TContext context)
        {
            Check.NotNull <TContext>(context, nameof(context));
            DatabaseExistenceState existenceState = new DatabaseTableChecker().AnyModelTableExists(context.InternalContext);

            if (existenceState == DatabaseExistenceState.Exists)
            {
                if (context.Database.CompatibleWithModel(true))
                {
                    return;
                }
                context.Database.Delete();
                existenceState = DatabaseExistenceState.DoesNotExist;
            }
            context.Database.Create(existenceState);
            this.Seed(context);
            context.SaveChanges();
        }
Example #3
0
        /// <summary>
        /// Executes the strategy to initialize the database for the given context.
        /// </summary>
        /// <param name="context"> The context. </param>
        public virtual void InitializeDatabase(TContext context)
        {
            Check.NotNull <TContext>(context, nameof(context));
            DatabaseExistenceState existenceState = new DatabaseTableChecker().AnyModelTableExists(context.InternalContext);

            if (existenceState == DatabaseExistenceState.Exists)
            {
                if (!context.Database.CompatibleWithModel(false, existenceState))
                {
                    throw Error.DatabaseInitializationStrategy_ModelMismatch((object)context.GetType().Name);
                }
            }
            else
            {
                context.Database.Create(existenceState);
                this.Seed(context);
                context.SaveChanges();
            }
        }
        /// <summary>
        ///     Executes the strategy to initialize the database for the given context.
        /// </summary>
        /// <param name="context"> The context. </param>
        public void InitializeDatabase(TContext context)
        {
            Check.NotNull(context, "context");

            var exists = new DatabaseTableChecker().AnyModelTableExists(context.InternalContext);

            if (_migrationsChecker.IsMigrationsConfigured(
                    context.InternalContext,
                    () =>
            {
                if (exists && !context.Database.CompatibleWithModel(throwIfNoMetadata: false))
                {
                    throw Error.DatabaseInitializationStrategy_ModelMismatch(context.GetType().Name);
                }

                return(exists);
            }))
            {
                return;
            }

            if (exists)
            {
                // If there is no metadata either in the model or in the database, then
                // we assume that the database matches the model because the common cases for
                // these scenarios are database/model first and/or an existing database.
                if (!context.Database.CompatibleWithModel(throwIfNoMetadata: false))
                {
                    throw Error.DatabaseInitializationStrategy_ModelMismatch(context.GetType().Name);
                }
            }
            else
            {
                context.Database.Create(skipExistsCheck: true);
                Seed(context);
                context.SaveChanges();
            }
        }
Example #5
0
        /// <summary>
        /// Executes the strategy to initialize the database for the given context.
        /// </summary>
        /// <param name="context"> The context. </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="context" />
        /// is
        /// <c>null</c>
        /// .
        /// </exception>
        public virtual void InitializeDatabase(TContext context)
        {
            Check.NotNull(context, "context");

            var existence = new DatabaseTableChecker().AnyModelTableExists(context.InternalContext);

            if (existence == DatabaseExistenceState.Exists)
            {
                if (context.Database.CompatibleWithModel(throwIfNoMetadata: true))
                {
                    return;
                }

                context.Database.Delete();
                existence = DatabaseExistenceState.DoesNotExist;
            }

            // Database didn't exist or we deleted it, so we now create it again.
            context.Database.Create(existence);

            Seed(context);
            context.SaveChanges();
        }
Example #6
0
        /// <summary>
        ///     Executes the strategy to initialize the database for the given context.
        /// </summary>
        /// <param name="context"> The context. </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="context" />
        ///     is
        ///     <c>null</c>
        ///     .
        /// </exception>
        public void InitializeDatabase(TContext context)
        {
            Check.NotNull(context, "context");

            var exists = new DatabaseTableChecker().AnyModelTableExists(context.InternalContext);

            if (_migrationsChecker.IsMigrationsConfigured(
                    context.InternalContext,
                    () =>
            {
                if (exists && !context.Database.CompatibleWithModel(throwIfNoMetadata: true))
                {
                    throw Error.DatabaseInitializationStrategy_ModelMismatch(context.GetType().Name);
                }

                return(exists);
            }))
            {
                return;
            }

            if (exists)
            {
                if (context.Database.CompatibleWithModel(throwIfNoMetadata: true))
                {
                    return;
                }

                context.Database.Delete();
            }

            // Database didn't exist or we deleted it, so we now create it again.
            context.Database.Create(skipExistsCheck: true);

            Seed(context);
            context.SaveChanges();
        }
Example #7
0
        /// <summary>
        /// Executes the strategy to initialize the database for the given context.
        /// </summary>
        /// <param name="context"> The context. </param>
        public virtual void InitializeDatabase(TContext context)
        {
            Check.NotNull(context, "context");

            var existence = new DatabaseTableChecker().AnyModelTableExists(context.InternalContext);

            if (existence == DatabaseExistenceState.Exists)
            {
                // If there is no metadata either in the model or in the database, then
                // we assume that the database matches the model because the common cases for
                // these scenarios are database/model first and/or an existing database.
                if (!context.Database.CompatibleWithModel(throwIfNoMetadata: false, existenceState: existence))
                {
                    throw Error.DatabaseInitializationStrategy_ModelMismatch(context.GetType().Name);
                }
            }
            else
            {
                // Either the database doesn't exist, or exists and is considered empty
                context.Database.Create(existence);
                Seed(context);
                context.SaveChanges();
            }
        }