示例#1
0
        private static SqlServerOptionsExtension GetOrCreateExtension(EntityOptionsBuilder optionsBuilder)
        {
            var existing = optionsBuilder.Options.FindExtension <SqlServerOptionsExtension>();

            return(existing != null
                ? new SqlServerOptionsExtension(existing)
                : new SqlServerOptionsExtension());
        }
        private static SqliteOptionsExtension GetOrCreateExtension(EntityOptionsBuilder options)
        {
            var existingExtension = options.Options.FindExtension <SqliteOptionsExtension>();

            return(existingExtension != null
                ? new SqliteOptionsExtension(existingExtension)
                : new SqliteOptionsExtension());
        }
        public static RelationalEntityOptionsBuilder UseSqlite([NotNull] this EntityOptionsBuilder options, [NotNull] DbConnection connection)
        {
            Check.NotNull(options, nameof(options));
            Check.NotNull(connection, nameof(connection));

            var extension = GetOrCreateExtension(options);

            extension.Connection = connection;
            ((IOptionsBuilderExtender)options).AddOrUpdateExtension(extension);

            return(new RelationalEntityOptionsBuilder(options));
        }
示例#4
0
        public static SqlServerEntityOptionsBuilder UseSqlServer([NotNull] this EntityOptionsBuilder optionsBuilder, [NotNull] string connectionString)
        {
            Check.NotNull(optionsBuilder, nameof(optionsBuilder));
            Check.NotEmpty(connectionString, nameof(connectionString));

            var extension = GetOrCreateExtension(optionsBuilder);

            extension.ConnectionString = connectionString;

            ((IOptionsBuilderExtender)optionsBuilder).AddOrUpdateExtension(extension);

            return(new SqlServerEntityOptionsBuilder(optionsBuilder));
        }
示例#5
0
        private IDbContextServices InitializeServices(IServiceProvider serviceProvider, EntityOptions options)
        {
            if (_initializing)
            {
                throw new InvalidOperationException(Strings.RecursiveOnConfiguring);
            }

            try
            {
                _initializing = true;

                var optionsBuilder = new EntityOptionsBuilder(options);
                OnConfiguring(optionsBuilder);

                var dataStores = serviceProvider?.GetService <IEnumerable <IDataStoreSource> >()?.ToList()
                                 ?? new List <IDataStoreSource>();

                if (dataStores.Count == 1 &&
                    !dataStores[0].IsConfigured(optionsBuilder.Options))
                {
                    dataStores[0].AutoConfigure(optionsBuilder);
                }

                var providerSource = serviceProvider != null ? ServiceProviderSource.Explicit : ServiceProviderSource.Implicit;

                serviceProvider = serviceProvider ?? ServiceProviderCache.Instance.GetOrAdd(optionsBuilder.Options);

                _loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();
                _logger        = _loggerFactory.CreateLogger <DbContext>();
                _logger.LogDebug(Strings.DebugLogWarning(nameof(LogLevel.Debug), nameof(ILoggerFactory) + "." + nameof(ILoggerFactory.MinimumLevel), nameof(LogLevel) + "." + nameof(LogLevel.Verbose)));

                var scopedServiceProvider = serviceProvider
                                            .GetRequiredService <IServiceScopeFactory>()
                                            .CreateScope()
                                            .ServiceProvider;

                return(scopedServiceProvider
                       .GetRequiredService <IDbContextServices>()
                       .Initialize(scopedServiceProvider, optionsBuilder.Options, this, providerSource));
            }
            finally
            {
                _initializing = false;
            }
        }
示例#6
0
 /// <summary>
 ///     Override this method to configure the data store (and other options) to be used for this context.
 ///     This method is called for each instance of the context that is created.
 /// </summary>
 /// <remarks>
 ///     If you passed an instance of <see cref="EntityOptions" /> to the constructor of the context (or
 ///     provided an <see cref="IServiceProvider" /> with <see cref="EntityOptions" /> registered) then
 ///     it is cloned before being passed to this method. This allows the options to be altered without
 ///     affecting other context instances that are constructed with the same <see cref="EntityOptions" />
 ///     instance.
 /// </remarks>
 /// <param name="optionsBuilder">
 ///     A builder used to create or modify options for this context. Data stores (and other extensions)
 ///     typically define extension methods on this object that allow you to configure the context.
 /// </param>
 protected internal virtual void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
 }
示例#7
0
 public static void UseInMemoryStore([NotNull] this EntityOptionsBuilder optionsBuilder, bool persist = true)
 => ((IOptionsBuilderExtender)Check.NotNull(optionsBuilder, nameof(optionsBuilder)))
 .AddOrUpdateExtension(
     new InMemoryOptionsExtension {
     Persist = persist
 });