Visual studio 2015 ASP.NET 5、实体框架7和;SQL Server 2014-多数据库上下文、SQL视图
我在Visual Studio 2015中创建了一个解决方案,包括一个Web项目(使用ASP.NET 5 Beta 6 Web应用程序预览模板)和一个模型项目(使用Entity Framework 7 Beta 6) 我可以从SQLServer数据库中获取数据并将其显示在页面上。然后,我想尝试从两个不同的数据库中提取数据,但在尝试使用多个dbContext时遇到了问题 在Startup.cs中,我添加了EF服务并添加了dbContext,这很好。但我不知道如何添加多个。。。下面的代码是我如何尝试添加另一个,但第二个AddDbContext似乎只是覆盖了第一个 我还遇到了另一个与此相关的问题。。我无法从SQL视图获取数据。我已经确认该视图确实包含数据,但当我编写WebAPI方法从该视图获取数据时,响应不包含任何内容。在过去使用EF6的项目中,我对视图建模和从中检索数据没有问题 总结我的问题:Visual studio 2015 ASP.NET 5、实体框架7和;SQL Server 2014-多数据库上下文、SQL视图,visual-studio-2015,dbcontext,asp.net-core,sql-server-2014,entity-framework-core,Visual Studio 2015,Dbcontext,Asp.net Core,Sql Server 2014,Entity Framework Core,我在Visual Studio 2015中创建了一个解决方案,包括一个Web项目(使用ASP.NET 5 Beta 6 Web应用程序预览模板)和一个模型项目(使用Entity Framework 7 Beta 6) 我可以从SQLServer数据库中获取数据并将其显示在页面上。然后,我想尝试从两个不同的数据库中提取数据,但在尝试使用多个dbContext时遇到了问题 在Startup.cs中,我添加了EF服务并添加了dbContext,这很好。但我不知道如何添加多个。。。下面的代码是我如何尝试
Startup.cs:
公共IConfiguration配置{get;set;}
公开启动(IHostingEnvironment env、IApplicationEnvironment appEnv)
{
//设置配置源。
var builder=新配置生成器(appEnv.ApplicationBasePath)
.AddJsonFile(“config.json”)
.AddenEnvironmentVariables();
Configuration=builder.Build();
}
//此方法由运行时调用。
//使用此方法向容器中添加服务
public void配置服务(IServiceCollection服务)
{
//注册实体框架
services.AddEntityFramework()
.AddSqlServer()文件
.AddDbContext(选项=>
选项。使用SQLServer(配置[“数据:默认连接:AconConnectionString”])
)
.AddDbContext(选项=>
选项。使用SQLServer(配置[“数据:默认连接:BConnectionString”])
);
services.AddMvc();
}
config.json
{
"ConnectionStrings": {
"Connection1": "[connection string here]",
"Connection2": "[connection string here]",
}
}
{
“数据”:{
“默认连接”:{
“A连接字符串”:“数据源=xxxxxxxx;初始目录=xxxxxxxx;Uid=xxxxxxxxx;Pwd=xxxxxx;”,
“B连接字符串”:“数据源=xxxxxxxx;初始目录=xxxxxxxx;Uid=xxxxxxxxx;Pwd=xxxxxx;”
}
},
“实体框架”:{
“MyContextA”:{
“ConnectionStringKey”:“数据:默认连接:A连接字符串”
},
“MyContextB”:{
“ConnectionStringKey”:“数据:默认连接:BConnectionString”
}
}
}
MyContextA.cs
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddDbContext<Context1>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection1"]));
services.AddDbContext<Context2>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection2"]));
// ...
}
公共类MyContextA:DbContext
{
公共MyContextA(DbContextOptions选项):基本(选项){}
公共DbSet乘积{get;set;}
公共DbSet Some_视图{get;set;}
}
MyContextB.cs
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddDbContext<Context1>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection1"]));
services.AddDbContext<Context2>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection2"]));
// ...
}
公共类MyContextB:DbContext
{
公共MyContextB(DbContextOptions选项):基本(选项){}
公共数据库集客户{get;set;}
}
Product.cs
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddDbContext<Context1>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection1"]));
services.AddDbContext<Context2>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection2"]));
// ...
}
公共类产品
{
[关键]
公共int ID{get;set;}
公共int名称{get;set;}
}
Some_View.cs
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddDbContext<Context1>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection1"]));
services.AddDbContext<Context2>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection2"]));
// ...
}
公共类某些视图
{
公共int SomeField{get;set;}
公共int另一个字段{get;set;}
}
TestController.cs
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddDbContext<Context1>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection1"]));
services.AddDbContext<Context2>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection2"]));
// ...
}
[路由(“api/[控制器]”)]
公共类TestController:控制器
{
私有只读MyContextA_dbContext;
//建造师
公共测试控制器(MyContextA dbContext)
{
_dbContext=dbContext;
}
//获取:api/值
[HttpGet]
公共IEnumerable Get()
{
var data=\u dbContext.Some\u视图;
返回数据;
}
}
我能够使用一个dbContext并使用常规表(如上面示例中的Product)使所有内容正常工作。但是当我尝试使用视图时,我没有得到任何数据,当我使用多个上下文时,第二个上下文将覆盖第一个上下文。尝试将
DbContext的构造函数更改为如下所示:
publicmycontexta(DbContextOptions选项):基本(选项){
公共MyContextB(DbContextOptions选项):基本(选项){}
尝试调用ToList()
返回数据。ToList();
这些漏洞中的每一个都可能是您希望在和回购协议上归档的漏洞。如果这对任何人都有帮助,那么.NET Core和EF Core 1.0.0就不再是问题了。下面是我如何在Web API Startup.cs中注册每个dbcontext的
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddDbContext<Context1>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection1"]));
services.AddDbContext<Context2>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection2"]));
// ...
}
什么“事情就是不能正常工作”?你希望看到什么样的行为?错误消息?等等。当我使用视图时,没有错误消息,只是响应中没有数据。另外,如果我在Startup.cs中添加两个dbcontext,它将使用第二个上下文而不是第一个上下文。ToList()抛出异常,编辑构造函数似乎不会影响任何事情。感谢您的建议,我们也将查看EF和MVC回购协议。看起来还没有。它支持视图。在查询过程中,它们和表没有什么不同,如果它们是可更新的视图,在更新过程中也没有什么不同。通过为DbContextOptions应用特定类型,解决了我的问题。如果没有DbContextOptions&DbContextOptions,MyContextB将覆盖StartUp.cs中的MyContextA。