本文介绍了在ASP.NET vNext和EF7多dbContexts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MVC 6 EF7与ASP.NET vNext建设网站系统相处。我期待在本教程:http://stephenwalther.com/archive/2015/01/17/asp-net-5-and-angularjs-part-4-using-entity-framework-7

I'm trying to get along with building web systems with ASP.NET vNext using MVC 6 and EF7. I'm looking at this tutorial: http://stephenwalther.com/archive/2015/01/17/asp-net-5-and-angularjs-part-4-using-entity-framework-7

在该页面,你会看到如何一的DbContext添加到项目和它的注册在这样的启动文件:

On the page you'll see how to add a dbContext to a project and it's registered in the startup file like this:

// Register Entity Framework
services.AddEntityFramework(Configuration)
        .AddSqlServer()
        .AddDbContext<MoviesAppContext>();

和上下文类看起来是这样的:

And the context class looks like this:

public class MoviesAppContext:DbContext
{
    public DbSet<Movie> Movies { get; set; }
}

它所有的工作不错,但现在我需要增加额外的DbContext的。虽然我不知道如何注册这个附加的上下文,使其由EF和可能被用来在我的项目中使用。

It all works good, but now I'm in need of adding an additional DbContext. Though I don't know how to register this additional context so that it will be used by EF and possible to use in my project.

比方说,我创建了这样一个新的上下文:

Let's say I've created a new context like this:

public class MyNewSuper:DbContext
{
    public DbSet<Model1> Model1 { get; set; }
    public DbSet<Model2> Model2 { get; set; }
}

我如何继续将其注册用在我的项目呢?

How do I go ahead to register it for use in my project then?

推荐答案

重要提示:配置实体框架7服务的语法以来这个职位,这是准确的,因为过去几年的变化测试版回合。同样的想法应该仍适用于新的语法虽然。

Important Note: The syntax for configuring the Entity Framework 7 services has changed since this post, which was accurate as of the last few beta rounds. The same idea should still apply to the new syntax though.

下面是我一直在做什么:

Here is what I've been doing:

services.AddEntityFramework().AddSqlServer()
                .AddDbContext<DataContextA>(options => options.UseSqlServer(Configuration.Get("StorageSettings:SQLConnectionString")))
                .AddDbContext<DataContextB>(options => options.UseSqlServer(Configuration.Get("StorageSettings:SQLConnectionString")));

其中, StorageSettings:SQLConnectionString 是一个SQL防爆preSS数据库的连接字符串。目前,我都DataContextA和DataContextB共享同一个数据库,但是你可以让他们分开。如果你想使用配置方法(这我不知道,pretty的爽!),你可以做这样的事情,以保持:

where StorageSettings:SQLConnectionString is a connection string for a SQL Express database. Currently, I have both DataContextA and DataContextB sharing the same database, but you can keep them separate. If you want to keep using the Configuration method (which I wasn't aware of, pretty cool!) you could do something like this:

{
    "Data": {
        "DefaultConnectionA": {
            "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=ContextADatabase;Trusted_Connection=True;MultipleActiveResultSets=true",
        "DefaultConnectionB": {
            "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=ContextBDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
    },
    "EntityFramework": {
        "DataContextA": {
            "ConnectionStringKey": "Data:DefaultConnectionA:ConnectionString"
        }
        "DataContextB": {
            "ConnectionStringKey": "Data:DefaultConnectionB:ConnectionString"
        }
    }
}

services.AddEntityFramework(Configuration)
                .AddSqlServer()
                .AddDbContext<DataContextA>()
                .AddDbContext<DataContextB>();

两者 DataContextA DataContextB 可注入控制器:

public class MyController: Controller {
    public MyController(DataContextA dataA, DataContextB dataB) {
        // Do stuff
    }
}

这篇关于在ASP.NET vNext和EF7多dbContexts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:19