示例#1
0
        /// <summary>
        /// Main constructor. All other constructors should call this one.
        /// For more information about <see cref="Microsoft.EntityFrameworkCore.DbContext" /> see the architecture of entity framework.
        /// </summary>
        /// <param name="dbContextOptions">The options to set up the db context.</param>
        /// <param name="hooks">The database hooks object to use. Can be null if no hooks are required. The passed object will not be changed but cloned.</param>
        /// <inheritdoc />
        protected HookedStateDataContext(
            [NotNull] DbContextOptions dbContextOptions,
            [CanBeNull] DataContextHookCollection <TDataContext, TState> hooks)
            : base(dbContextOptions)
        {
            this.hooks = hooks?.Clone();
            this.hooks?.ForAll().AddPreEventAsyncHandler((db, state, entities, type) =>
            {
                if (type == HookEventType.Delete)
                {
                    return(Task.FromResult(false));
                }

                OnPreHook(db, state, entities, type);

                return(Task.FromResult(true));
            });
        }
示例#2
0
        /// <summary>
        /// Main constructor. All other constructors should call this one.
        /// For more information about <see cref="System.Data.Entity.DbContext" /> see the architecture of entity framework.
        /// </summary>
        /// <param name="nameOrConnectionString">The connection string to use for the connection to the database.</param>
        /// <param name="hooks">The database hooks object to use. Can be null if no hooks are required. The passed object will not be changed but cloned.</param>
        /// <inheritdoc />
        public HookedStateDataContext(
            [NotNull] string nameOrConnectionString,
            [CanBeNull] DataContextHookCollection <TDataContext, TState> hooks)
            : base(nameOrConnectionString)
        {
            Database.CommandTimeout = 60;

            this.hooks = hooks?.Clone();
            this.hooks?.ForAll().AddPreEventAsyncHandler((db, state, entities, type) =>
            {
                if (type == HookEventType.Delete)
                {
                    return(Task.FromResult(false));
                }

                OnPreHook(db, state, entities, type);

                return(Task.FromResult(true));
            });

            Configuration.AutoDetectChangesEnabled = true;

            Database.SetInitializer <TDataContext>(null);
        }