Ef Core

There are two ways to configure the database:

 1, rewrite  DbContext.OnConfiguring(DbContextOptionsBuilder) method

class MyDBContext : DBContext { ... protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"connectionString"); } }
2, by  DbContextOptionsBuilder creating a class  DbContextOptionsinstance, and the constructor  DbContext(DbContextOptions) is passed to the DbContext
DbContext 必须具有的实例DbContextOptions才能执行任何工作
  • Database provider, to use, usually by calling the selection method, such as UseSqlServeror UseSqliteThese methods require extensions to provide appropriate package, such as Microsoft.EntityFrameworkCore.SqlServeror Microsoft.EntityFrameworkCore.SqliteThe method defined Microsoft.EntityFrameworkCorenamespace.
  • Any necessary connection string or identifier of the database instance, typically passed as a parameter to the above-described method of providing program selection
  • Any provider-level optional behavior selector, usually linked to the provider selection method invocation
  • Any conventional EF Core behavior choices, providing the program selector method before or after the usual link

The following example will configure DbContextOptionsto use SQL Server provider, in connection contains connectionStringvariables, the provider level command timeout, and allows all queries EF Core behavior selector in the implementation of DbContextNo tracking by default:

optionsBuilder
    .UseSqlServer(connectionString, providerOptions=>providerOptions.CommandTimeout(60))
    .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

options = new DbContextOptionsBuilder<CholessContext>()
 .UseInMemoryDatabase("choless") .Options; using (var context = new MyDBContext(options)) { ... }

ABP的封装,引用Volo.Abp.EntityFrameworkCore,以及具体的数据库,
它的步骤在Configure(ServiceConfigurationContext context)方法 执行Configure<AbpDbContextOptions>(option=>option.UseSqlServer()),
AbpDbContextOptions定义的方法Configure以及Configure<TDbContext>,
是对AbpDbContextConfigurationContext.DbContextOptions(DbContextOptionsBuilder).UseSqlServer(字符串)进行设置,相应字符串,在工厂创建
AbpDbContextConfigurationContext,引入IConnectionStringResolver,解析字符串
 

AbpDbContext registration service contexts, including the default warehouse, the particular storage, and DbContext Alternatively, (commissioned)

will DbContextOptions <TDbContext> into the container inside the factory method for creating DbContext
and this DbContext, is provided by UnitOfWorkDbContextProvider,
public static IServiceCollection AddAbpDbContext<TDbContext>(
            this IServiceCollection services, 
            Action<IAbpDbContextRegistrationOptionsBuilder> optionsBuilder = null)
            where TDbContext : AbpDbContext<TDbContext>
        {
            services.AddMemoryCache();

            var options = new AbpDbContextRegistrationOptions(typeof(TDbContext), services);
            optionsBuilder?.Invoke(options);

            services.TryAddTransient(DbContextOptionsFactory.Create<TDbContext>);

            foreach (var dbContextType in options.ReplacedDbContextTypes)
            {
                services.Replace(ServiceDescriptor.Transient(dbContextType, typeof(TDbContext)));
            }

            new EfCoreRepositoryRegistrar(options).AddRepositories();

            return services;
        }
 

AbpDbContextRegistrationOptions : use IServicsCollection to replace the default storage configuration for a particular entity, as well as the DbContext, this function is very powerful

 

AbpEntityOptions:

 

 DbContextCreationContext : Only connectionStringName, connectionString packaging, there is Use method (release the old DbContextCreationContext)

AbpDbContextConfigurationContext:只有ConnectionString、ConnectionStringName、DbContextOptionsBuilder、DbConnection包装

AbpDbContextOptions:  =>对AbpDbContextConfigurationContext委托 的配置,  List<Action<AbpDbContextConfigurationContext >>,我们使用主要到这个AbpDbContextOptions对里面AbpDbContextConfigurationContext委托配置,

它在DbContextOptionsFactory.Create方法,创建DbContextOptions<TDbContext>,而这个参数用于创建AbpDbContext实例

它的操作步骤是它获取DbContextCreationContext(ConnectionStringNameAttribute上的connectionStringName,使用IConnectionStringResolver解析到字符串)

创建new AbpDbContextConfigurationContext<TDbContext>()

执行PreConfigure、Configure的方法(每个都是Default通用方法,以及具体的AbpDbContextConfigurationContext<TDbContext>的方法)

返回到AbpDbContextConfigurationContext.DbContextOptions.Options,即是 DbContextOptions<TDbContext>

而这个Option的创建工厂加入到IServiceCollection容器里面

而自身创建XXXDbContext,它需要它需要继承自 AbpDbContext<T>,此类的是派生于ITransientDependency,在依赖注入系统会自动注入XXXDbContext,在IDbContextProvider<TDbContext>,它的实现,

是提供具体的XXXDbContext,要注意是一个工作单元里,里面也存储一个实例字典实现方便使用,它是随着工作单元的释放也释放

 context.Services.TryAddTransient(typeof(IDbContextProvider<>), typeof(UnitOfWorkDbContextProvider<>));

 

 

 

 

AbpModelBuilderConfigurationOptions:

 

 

AbpDbContextConfigurationContext :DbContextOptionsBuilder

扩展方法:UseUseMySQL

 

DbContextCreationContext

 

 

 

 

 

 

AbpDbContextRegistrationOptions:

AbpEntityOptions:

AbpDbContextOptions:

AbpModelBuilderConfigurationOptions:

 

 

AbpDbContextConfigurationContext

DbContextCreationContext

 

 

 

 

 

 

AbpDbContextRegistrationOptions:

AbpEntityOptions:

AbpDbContextOptions:

AbpModelBuilderConfigurationOptions:

 

 

AbpDbContextConfigurationContext

DbContextCreationContext

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/cloudsu/p/11942981.html