Esempio n. 1
0
        /// <summary>
        ///     <para>
        ///         Adds the services required by the SQLite 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)
        ///          {
        ///              var connectionString = "connection string to database";
        ///
        ///              services
        ///                  .AddEntityFrameworkSqlite()
        ///                  .AddDbContext&lt;MyContext&gt;((serviceProvider, options) =>
        ///                      options.UseSqlite(connectionString)
        ///                             .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 AddEntityFrameworkSqlite([NotNull] this IServiceCollection serviceCollection)
        {
            Check.NotNull(serviceCollection, nameof(serviceCollection));

            var serviceCollectionMap = new ServiceCollectionMap(serviceCollection)
                                       .TryAddSingletonEnumerable <IDatabaseProvider, DatabaseProvider <SqliteOptionsExtension> >()
                                       .TryAddSingleton <IRelationalAnnotationProvider, SqliteAnnotationProvider>()
                                       .TryAddSingleton <IRelationalTypeMapper, SqliteTypeMapper>()
                                       .TryAddSingleton <ISqlGenerationHelper, SqliteSqlGenerationHelper>()
                                       .TryAddSingleton <IMigrationsAnnotationProvider, SqliteMigrationsAnnotationProvider>()
                                       .TryAddScoped <IConventionSetBuilder, SqliteConventionSetBuilder>()
                                       .TryAddScoped <IUpdateSqlGenerator, SqliteUpdateSqlGenerator>()
                                       .TryAddScoped <IModificationCommandBatchFactory, SqliteModificationCommandBatchFactory>()
                                       .TryAddScoped <ISqliteRelationalConnection, SqliteRelationalConnection>()
                                       .TryAddScoped <IRelationalConnection>(p => p.GetService <ISqliteRelationalConnection>())
                                       .TryAddScoped <IMigrationsSqlGenerator, SqliteMigrationsSqlGenerator>()
                                       .TryAddScoped <IRelationalDatabaseCreator, SqliteDatabaseCreator>()
                                       .TryAddScoped <IHistoryRepository, SqliteHistoryRepository>()
                                       .TryAddScoped <IMemberTranslator, SqliteCompositeMemberTranslator>()
                                       .TryAddScoped <IMethodCallTranslator, SqliteCompositeMethodCallTranslator>()
                                       .TryAddScoped <IQuerySqlGeneratorFactory, SqliteQuerySqlGeneratorFactory>();

            ;

            ServiceCollectionRelationalProviderInfrastructure.TryAddDefaultRelationalServices(serviceCollectionMap);

            return(serviceCollection);
        }
        /// <summary>
        ///     <para>
        ///         Adds the services required by the Microsoft SQL Server 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)
        ///           {
        ///               var connectionString = "connection string to database";
        ///
        ///               services
        ///                   .AddEntityFrameworkSqlServer()
        ///                   .AddDbContext&lt;MyContext&gt;((serviceProvider, options) =>
        ///                       options.UseSqlServer(connectionString)
        ///                              .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 AddEntityFrameworkSqlServer([NotNull] this IServiceCollection serviceCollection)
        {
            Check.NotNull(serviceCollection, nameof(serviceCollection));

            var serviceCollectionMap = new ServiceCollectionMap(serviceCollection)
                                       .TryAddSingletonEnumerable <IDatabaseProvider, DatabaseProvider <SqlServerOptionsExtension> >()
                                       .TryAddSingleton <ISqlServerValueGeneratorCache, SqlServerValueGeneratorCache>()
                                       .TryAddSingleton <IValueGeneratorCache>(p => p.GetService <ISqlServerValueGeneratorCache>())
                                       .TryAddSingleton <IRelationalTypeMapper, SqlServerTypeMapper>()
                                       .TryAddSingleton <ISqlGenerationHelper, SqlServerSqlGenerationHelper>()
                                       .TryAddSingleton <IRelationalAnnotationProvider, SqlServerAnnotationProvider>()
                                       .TryAddSingleton <IMigrationsAnnotationProvider, SqlServerMigrationsAnnotationProvider>()
                                       .TryAddScoped <IRelationalValueBufferFactoryFactory, UntypedRelationalValueBufferFactoryFactory>()
                                       .TryAddScoped <IModelValidator, SqlServerModelValidator>()
                                       .TryAddScoped <IConventionSetBuilder, SqlServerConventionSetBuilder>()
                                       .TryAddScoped <ISqlServerUpdateSqlGenerator, SqlServerUpdateSqlGenerator>()
                                       .TryAddScoped <IUpdateSqlGenerator>(p => p.GetService <ISqlServerUpdateSqlGenerator>())
                                       .TryAddScoped <ISqlServerSequenceValueGeneratorFactory, SqlServerSequenceValueGeneratorFactory>()
                                       .TryAddScoped <IModificationCommandBatchFactory, SqlServerModificationCommandBatchFactory>()
                                       .TryAddScoped <IValueGeneratorSelector, SqlServerValueGeneratorSelector>()
                                       .TryAddScoped <ISqlServerConnection, SqlServerConnection>()
                                       .TryAddScoped <IRelationalConnection>(p => p.GetService <ISqlServerConnection>())
                                       .TryAddScoped <IMigrationsSqlGenerator, SqlServerMigrationsSqlGenerator>()
                                       .TryAddScoped <IRelationalDatabaseCreator, SqlServerDatabaseCreator>()
                                       .TryAddScoped <IHistoryRepository, SqlServerHistoryRepository>()
                                       .TryAddScoped <IEntityQueryModelVisitorFactory, SqlServerQueryModelVisitorFactory>()
                                       .TryAddScoped <ICompiledQueryCacheKeyGenerator, SqlServerCompiledQueryCacheKeyGenerator>()
                                       .TryAddScoped <IExecutionStrategyFactory, SqlServerExecutionStrategyFactory>()
                                       .TryAddScoped <IQueryCompilationContextFactory, SqlServerQueryCompilationContextFactory>()
                                       .TryAddScoped <IMemberTranslator, SqlServerCompositeMemberTranslator>()
                                       .TryAddScoped <IMethodCallTranslator, SqlServerCompositeMethodCallTranslator>()
                                       .TryAddScoped <IQuerySqlGeneratorFactory, SqlServerQuerySqlGeneratorFactory>();

            ServiceCollectionRelationalProviderInfrastructure.TryAddDefaultRelationalServices(serviceCollectionMap);

            return(serviceCollection);
        }
        public static IServiceCollection AddEntityFrameworkRelationalDatabase(IServiceCollection serviceCollection)
        {
            serviceCollection.TryAddEnumerable(
                ServiceDescriptor.Singleton <IDatabaseProvider, DatabaseProvider <FakeRelationalOptionsExtension> >());

            serviceCollection.TryAdd(new ServiceCollection()
                                     .AddSingleton <ISqlGenerationHelper, RelationalSqlGenerationHelper>()
                                     .AddSingleton <IRelationalTypeMapper, TestRelationalTypeMapper>()
                                     .AddSingleton <IRelationalAnnotationProvider, TestAnnotationProvider>()
                                     .AddScoped <IMigrationsSqlGenerator, TestRelationalMigrationSqlGenerator>()
                                     .AddScoped <IConventionSetBuilder, TestRelationalConventionSetBuilder>()
                                     .AddScoped <IMemberTranslator, TestRelationalCompositeMemberTranslator>()
                                     .AddScoped <IMethodCallTranslator, TestRelationalCompositeMethodCallTranslator>()
                                     .AddScoped <IQuerySqlGeneratorFactory, TestQuerySqlGeneratorFactory>()
                                     .AddScoped <IRelationalConnection, FakeRelationalConnection>()
                                     .AddScoped <IHistoryRepository>(_ => null)
                                     .AddScoped <IUpdateSqlGenerator>(_ => null)
                                     .AddScoped <IModificationCommandBatchFactory>(_ => null)
                                     .AddScoped <IRelationalDatabaseCreator>(_ => null));

            ServiceCollectionRelationalProviderInfrastructure.TryAddDefaultRelationalServices(new ServiceCollectionMap(serviceCollection));

            return(serviceCollection);
        }