Beispiel #1
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");

            if (_migrationsChecker.IsMigrationsConfigured(
                    context.InternalContext,
                    () => new DatabaseTableChecker().AnyModelTableExists(context.InternalContext)))
            {
                return;
            }

            context.Database.Delete();
            context.Database.Create();
            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();
            }
        }
Beispiel #3
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();
        }