简体   繁体   English

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

[英]Does DBContext in startup.cs of .netcore Represent the entire database in the ASP application?

Is multiple DbContext allowed or it is supposed to be only one in an entire ASP application? 是否允许使用多个DbContext或者在整个ASP应用程序中只能使用一个DbContext

Code Snippet from my Startup.cs 我的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())

Clearly, it's allowed. 显然,这是允许的。 If they are pointing at two different databases, it's even necessary. 如果他们指向两个不同的数据库,则甚至有必要。 But even if not, it can be a useful way of differentiating the dependencies when they are consumed. 但是,即使不是,它也可能是区分依赖项时的一种有用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用DbContext的Singleton - 在Startup.cs中创建实例 - Singleton with DbContext - creating instance in Startup.cs ASP.NET 核心空白应用startup.cs - ASP.NET Core blank application startup.cs 如何在startup.cs的ConfigureServices方法中正确注入DbContext实例(ASP.net core 1.1)? - How to inject DbContext instance in the ConfigureServices method of startup.cs correctly (ASP.net core 1.1)? 如何在不使用实体框架的情况下在 Startup.cs 中添加 DbContext? - How to add DbContext In Startup.cs without using Entity Framework? 如何在Startup.cs文件中获取ASP.NET MVC应用程序的基本URL? - How to get ASP.NET MVC application's base url in Startup.cs file? 如何在我的 ASP.NET Core 应用程序中使用 Startup.cs 之外的服务? - How can I use a service outside of Startup.cs in my ASP.NET Core Application? 在ASP.NET CORE中的Startup.cs中设置动态变量 - Set Dynamic Variables in Startup.cs in ASP.NET CORE 如何在 ASP.NET Core 中的 Startup.cs 中注册 RoleManager - How to register RoleManager in Startup.cs in ASP.NET Core 如何使用Async在ASP.NET 5 Startup.cs中为用户设定种子 - How to seed Users in ASP.NET 5 Startup.cs with Async ASP.NET MVC 中的 Startup.cs 文件未显示 - Startup.cs file in ASP.NET MVC is not shown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM