我在Visual Studio 2015中创建了一个解决方案,包括一个Web项目(使用ASP.NET 5 Beta 6 Web应用程序预览模板)和一个Model项目(使用Entity Framework 7 Beta 6)。
我已经能够从SQL Server数据库获取数据并在页面上显示。然后,我想尝试从两个不同的数据库中提取数据,但是在尝试使用多个dbContext时遇到了问题。
在Startup.cs中,我添加了EF服务并添加了一个dbContext,这很好用。但我不知道如何添加多个...下面的代码是我尝试添加另一个的方式,但第二个AddDbContext似乎只是覆盖了第一个。
我还遇到了另一个与此有关的问题..我无法从SQL视图中获取数据。我确认该视图包含数据,但当我编写一个WebAPI方法从该视图获取数据时,响应中什么也没有。在以前使用EF6的项目中,我没有问题对视图进行建模并从中检索数据。
总结我的问题:
1. 如何在使用EF7的ASP.NET 5 Web项目中设置多个dbContext? 2. 如何在EF7中对SQL Server视图进行建模?
Startup.cs:
我已经能够从SQL Server数据库获取数据并在页面上显示。然后,我想尝试从两个不同的数据库中提取数据,但是在尝试使用多个dbContext时遇到了问题。
在Startup.cs中,我添加了EF服务并添加了一个dbContext,这很好用。但我不知道如何添加多个...下面的代码是我尝试添加另一个的方式,但第二个AddDbContext似乎只是覆盖了第一个。
我还遇到了另一个与此有关的问题..我无法从SQL视图中获取数据。我确认该视图包含数据,但当我编写一个WebAPI方法从该视图获取数据时,响应中什么也没有。在以前使用EF6的项目中,我没有问题对视图进行建模并从中检索数据。
总结我的问题:
1. 如何在使用EF7的ASP.NET 5 Web项目中设置多个dbContext? 2. 如何在EF7中对SQL Server视图进行建模?
Startup.cs:
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
// Register Entity Framework
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyContextA>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:AConnectionString"])
)
.AddDbContext<MyContextB>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:BConnectionString"])
);
services.AddMvc();
}
config.json
{
"Data": {
"DefaultConnection": {
"AConnectionString": "data source=xxxxxxx;initial catalog=xxxxxxxx;Uid=xxxxxxxxx;Pwd=xxxxxx;",
"BConnectionString": "data source=xxxxxxx;initial catalog=xxxxxxxx;Uid=xxxxxxxxx;Pwd=xxxxxx;"
}
},
"EntityFramework": {
"MyContextA": {
"ConnectionStringKey": "Data:DefaultConnection:AConnectionString"
},
"MyContextB": {
"ConnectionStringKey": "Data:DefaultConnection:BConnectionString"
}
}
}
MyContextA.cs
public class MyContextA : DbContext
{
public MyContextA (DbContextOptions options) : base(options) { }
public DbSet<Product> Product { get; set; }
public DbSet<Some_View> Some_View { get; set; }
}
MyContextB.cs
public class MyContextB : DbContext
{
public MyContextB (DbContextOptions options) : base(options) { }
public DbSet<Customer> Customer { get; set; }
}
Product.cs
public class Product
{
[Key]
public int ID { get; set; }
public int Name { get; set; }
}
Some_View.cs
public class Some_View
{
public int SomeField { get; set; }
public int AnotherField { get; set; }
}
TestController.cs
[Route("api/[controller]")]
public class TestController : Controller
{
private readonly MyContextA _dbContext;
// Constructor
public TestController(MyContextA dbContext)
{
_dbContext = dbContext;
}
// GET: api/values
[HttpGet]
public IEnumerable<Some_View> Get()
{
var data = _dbContext.Some_View;
return data;
}
}
我能使用一个dbContext和常规表,比如上面的Product,使一切正常运作。但是当我尝试使用视图时,无法获取数据;当我使用多个contexts时,第二个context会覆盖第一个。