.netcore的startup.cs中的DBContext是否代表ASP应用程序中的整个数据库?

时间:2017-07-14 15:44:40

标签: c# entity-framework asp.net-core-mvc

是否允许多个DbContext,或者它应该只是整个ASP应用程序中的一个?

我的Startup.cs

中的代码段
public IConfigurationRoot Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
    services.AddDbContext<PatientContext> (options => options.UseSqlite("Data Source=MyDatabase.db"));
    services.AddDbContext<AdminAccountContext> (options => options.UseSqlite("Data Source=MyDatabase.db"));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())

1 个答案:

答案 0 :(得分:2)

显然,它是允许的。如果他们指向两个不同的数据库,甚至是必要的。但即使不是,它也可以成为区分依赖关系的有用方法。