Ef Core

有两种方式对数据库进行配置:

 1、重写 DbContext.OnConfiguring(DbContextOptionsBuilder) 方法

class MyDBContext : DBContext { ... protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"connectionString"); } }
2、通过 DbContextOptionsBuilder 类创建 DbContextOptions实例,并通过构造函数 DbContext(DbContextOptions) 传递给 DbContext
DbContext 必须具有的实例DbContextOptions才能执行任何工作
  • 数据库提供程序,若要使用,通常选择通过调用的方法,如UseSqlServerUseSqlite。 这些扩展方法需要相应的提供程序包,如Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Sqlite。 中定义的方法Microsoft.EntityFrameworkCore命名空间。
  • 任何必要的连接字符串或标识符的数据库实例中,通常作为参数传递到上述提供程序选择方法
  • 任何提供程序级别的可选行为选择器,通常还链接到提供程序选择方法调用中
  • 任何常规 EF Core 行为选择器,通常链接之后或之前提供程序选择器方法

下面的示例将配置DbContextOptions若要使用 SQL Server 提供程序,在连接包含connectionString变量、 提供程序级别的命令超时,以及可使在中执行的所有查询 EF Core 行为选择器DbContext否跟踪默认情况下:

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上下文进行服务进行注册,包括默认仓储,特定仓储,以及DbContext替换,(委托进行的)

将DbContextOptions<TDbContext>的工厂方法 注入容器里面,用于创建DbContext
而这个DbContext,是由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:利用IServicsCollection对默认仓储,特定实体的配置,以及DbContext的替换,这个功能已经很强大

 

AbpEntityOptions:

 

 DbContextCreationContext:只有connectionStringName、connectionString的包装,有 Use方法(对旧的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

猜你喜欢

转载自www.cnblogs.com/cloudsu/p/11942981.html