public void Calling_AddEntityFramework_explicitly_does_not_change_services()
        {
            var services1 = AddServices(new ServiceCollection());
            var services2 = AddServices(new ServiceCollection());

            ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(new ServiceCollectionMap(services2));

            AssertServicesSame(services1, services2);
        }
Exemplo n.º 2
0
        private static void ApplyServices(IDbContextOptions options, ServiceCollection services)
        {
            var coreServicesAdded = false;

            foreach (var extension in options.Extensions)
            {
                if (extension.ApplyServices(services))
                {
                    coreServicesAdded = true;
                }
            }

            if (!coreServicesAdded)
            {
                ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(
                    new ServiceCollectionMap(services));
            }
        }
        public void Throws_with_new_when_no_provider_use_Database()
        {
            var serviceCollection = new ServiceCollection();

            ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(new ServiceCollectionMap(serviceCollection));
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var options = new DbContextOptionsBuilder <ConstructorTestContext1A>()
                          .UseInternalServiceProvider(serviceProvider)
                          .Options;

            using (var context = new ConstructorTestContext1A(options))
            {
                Assert.Equal(
                    CoreStrings.NoProviderConfigured,
                    Assert.Throws <InvalidOperationException>(() => context.Database.GetDbConnection()).Message);
            }
        }
Exemplo n.º 4
0
        public void Throws_on_attempt_to_use_store_with_no_store_services()
        {
            var serviceCollection = new ServiceCollection();

            ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(new ServiceCollectionMap(serviceCollection));
            var serviceProvider = serviceCollection.BuildServiceProvider();

            Assert.Equal(
                CoreStrings.NoProviderConfigured,
                Assert.Throws <InvalidOperationException>(() =>
            {
                using (var context = new ImplicitConfigButNoServicesBlogContext(serviceProvider))
                {
                    context.Blogs.Add(new Blog {
                        Name = "The Waffle Cart"
                    });
                    context.SaveChanges();
                }
            }).Message);
        }
            public void Throws_on_attempt_to_use_store_with_no_store_services()
            {
                var serviceCollection = new ServiceCollection();

                ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(new ServiceCollectionMap(serviceCollection));
                var serviceProvider = serviceCollection.BuildServiceProvider();

                using (SqlServerNorthwindContext.GetSharedStore())
                {
                    Assert.Equal(
                        CoreStrings.NoProviderConfigured,
                        Assert.Throws <InvalidOperationException>(() =>
                    {
                        using (var context = new NorthwindContext(
                                   new DbContextOptionsBuilder()
                                   .UseInternalServiceProvider(serviceProvider).Options))
                        {
                            Assert.Equal(91, context.Customers.Count());
                        }
                    }).Message);
                }
            }
Exemplo n.º 6
0
        /// <summary>
        ///     <para>
        ///         Adds the services required by the in-memory database provider for Entity Framework
        ///         to an <see cref="IServiceCollection" />. You use this method when using dependency injection
        ///         in your application, such as with ASP.NET. For more information on setting up dependency
        ///         injection, see http://go.microsoft.com/fwlink/?LinkId=526890.
        ///     </para>
        ///     <para>
        ///         You only need to use this functionality when you want Entity Framework to resolve the services it uses
        ///         from an external dependency injection container. If you are not using an external
        ///         dependency injection container, Entity Framework will take care of creating the services it requires.
        ///     </para>
        /// </summary>
        /// <example>
        ///     <code>
        ///         public void ConfigureServices(IServiceCollection services)
        ///         {
        ///             services
        ///                 .AddEntityFrameworkInMemoryDatabase()
        ///                 .AddDbContext&lt;MyContext&gt;((serviceProvider, options) =>
        ///                     options.UseInMemoryDatabase()
        ///                            .UseInternalServiceProvider(serviceProvider));
        ///         }
        ///     </code>
        /// </example>
        /// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
        /// <returns>
        ///     The same service collection so that multiple calls can be chained.
        /// </returns>
        public static IServiceCollection AddEntityFrameworkInMemoryDatabase([NotNull] this IServiceCollection serviceCollection)
        {
            Check.NotNull(serviceCollection, nameof(serviceCollection));

            var serviceCollectionMap = new ServiceCollectionMap(serviceCollection)
                                       .TryAddSingletonEnumerable <IDatabaseProvider, DatabaseProvider <InMemoryOptionsExtension> >()
                                       .TryAddSingleton <IInMemoryStoreSource, InMemoryStoreSource>()
                                       .TryAddSingleton <IInMemoryTableFactory, InMemoryTableFactory>()
                                       .TryAddScoped <IValueGeneratorSelector, InMemoryValueGeneratorSelector>()
                                       .TryAddScoped <IInMemoryDatabase, InMemoryDatabase>()
                                       .TryAddScoped <IDatabase>(p => p.GetService <IInMemoryDatabase>())
                                       .TryAddScoped <IDbContextTransactionManager, InMemoryTransactionManager>()
                                       .TryAddScoped <IDatabaseCreator, InMemoryDatabaseCreator>()
                                       .TryAddScoped <IMaterializerFactory, MaterializerFactory>()
                                       .TryAddScoped <IQueryContextFactory, InMemoryQueryContextFactory>()
                                       .TryAddScoped <IEntityQueryModelVisitorFactory, InMemoryQueryModelVisitorFactory>()
                                       .TryAddScoped <IEntityQueryableExpressionVisitorFactory, InMemoryEntityQueryableExpressionVisitorFactory>();

            ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(serviceCollectionMap);

            return(serviceCollection);
        }
        public void Throws_with_add_when_no_provider_use_Database()
        {
            var serviceCollection = new ServiceCollection();

            ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(new ServiceCollectionMap(serviceCollection));

            var appServiceProivder = serviceCollection
                                     .AddDbContext <ConstructorTestContext1A>(
                (p, b) => b.UseInternalServiceProvider(p))
                                     .BuildServiceProvider();

            using (var serviceScope = appServiceProivder
                                      .GetRequiredService <IServiceScopeFactory>()
                                      .CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetService <ConstructorTestContext1A>();

                Assert.Equal(
                    CoreStrings.NoProviderConfigured,
                    Assert.Throws <InvalidOperationException>(() => context.Database.GetDbConnection()).Message);
            }
        }