Ejemplo n.º 1
0
        public DbContext([NotNull] ImmutableDbContextOptions configuration)
        {
            Check.NotNull(configuration, "configuration");

            InitializeSets(null, configuration);
            _configuration = new LazyRef <DbContextConfiguration>(() => Initialize(null, configuration));
        }
Ejemplo n.º 2
0
        public DbContext([NotNull] IServiceProvider serviceProvider, [NotNull] ImmutableDbContextOptions configuration)
        {
            Check.NotNull(serviceProvider, "serviceProvider");
            Check.NotNull(configuration, "configuration");

            InitializeSets(serviceProvider, configuration);
            _configuration = new LazyRef <DbContextConfiguration>(() => Initialize(serviceProvider, configuration));
        }
Ejemplo n.º 3
0
        private DbContextConfiguration Initialize(IServiceProvider serviceProvider, ImmutableDbContextOptions contextOptions)
        {
            var providerSource = serviceProvider != null
                ? DbContextConfiguration.ServiceProviderSource.Explicit
                : DbContextConfiguration.ServiceProviderSource.Implicit;

            serviceProvider = serviceProvider ?? ServiceProviderCache.Instance.GetOrAdd(contextOptions);

            _scopedServiceProvider = serviceProvider
                                     .GetService <IServiceScopeFactory>()
                                     .CreateScope()
                                     .ServiceProvider;

            return(_scopedServiceProvider
                   .GetService <DbContextConfiguration>()
                   .Initialize(serviceProvider, _scopedServiceProvider, contextOptions, this, providerSource));
        }
Ejemplo n.º 4
0
        public virtual IServiceProvider GetOrAdd(ImmutableDbContextOptions contextOptions)
        {
            var services = new ServiceCollection();
            var builder  = services.AddEntityFramework();

            foreach (var extension in contextOptions.Extensions)
            {
                extension.ApplyServices(builder);
            }

            // TODO: Consider more robust hashing algorithm
            // Note that no cryptographic algorithm is available on all of phone/store/k/desktop
            unchecked
            {
                var key = services.Aggregate(0, (t, d) => (t * 397) ^ CalculateHash(d).GetHashCode());

                return(_configurations.GetOrAdd(key, k => services.BuildServiceProvider()));
            }
        }
Ejemplo n.º 5
0
        private void InitializeSets(IServiceProvider serviceProvider, ImmutableDbContextOptions contextOptions)
        {
            serviceProvider = serviceProvider ?? ServiceProviderCache.Instance.GetOrAdd(contextOptions);

            serviceProvider.GetRequiredService <DbSetInitializer>().InitializeSets(this);
        }