ASP.NET 5,實體框架7和SQL Server 2014 - 多個dbContexts,SQL視圖

asp.net-core dbcontext entity-framework-core sql-server-2014 visual-studio-2015

我在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項目中設置多個dbContexts?
  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

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();
}

MyContextA.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();
}

MyContextB.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();
}

Product.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();
}

Some_View.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();
}

TestController.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();
}

我可以使用一個dbContext並使用常規表(例如上面的示例中的Product)來處理所有事情。但是當我嘗試使用視圖時,我得不到數據,當我使用多個上下文時,第二個上下文會覆蓋第一個上下文。

一般承認的答案

如果這可以幫助任何人,這不再是.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"]));
    // ...
}

appsettings.DEVELOPMENT.json

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddDbContext<Context1>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection1"]));
    services.AddDbContext<Context2>(options => options.UseSqlServer(Configuration["ConnectionStrings:Connection2"]));
    // ...
}

熱門答案

嘗試更改DbContext的構造函數,如下所示:

public MyContextA (DbContextOptions<MyContextA> options) : base(options) { }
public MyContextB (DbContextOptions<MyContextB> options) : base(options) { }

嘗試調用ToList()

public MyContextA (DbContextOptions<MyContextA> options) : base(options) { }
public MyContextB (DbContextOptions<MyContextB> options) : base(options) { }

這些中的每一個都可能是您要在EFMVC repos上提交的錯誤。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因