MVC 6 EF7 RC1创建多个dbcontexts

时间:2015-12-08 16:40:23

标签: asp.net-core-mvc entity-framework-core

我试图弄清楚如何在EF7 RC1中创建第二个DB上下文。在过去,我可以使用构造函数:base(" connectionName")但这似乎不再是一个选项,因为它说不能将字符串转换为System.IServiceProvider。

我的第二个上下文代码如下:

public class DecAppContext : DbContext
    {

        public DecAppContext()
          //  :base("DefaultConnection")
        {

        }
        public DbSet<VignetteModels> VignetteModels { get; set; }
        public DbSet<VignetteResult> Result { get; set; }
    }
}

在我的config.json中,我指定了连接:

"Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-xxxxx...;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  }

在我的启动的配置服务部分中,我添加了两个上下文:

services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
                .AddDbContext<DecAppContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

applicationDB上下文工作正常,因为我可以创建一个没有问题的用户和登录

但是,当我尝试通过以下方式访问控制器中的其他上下文时:

private DecAppContext db = new DecAppContext();
var vignette = db.VignetteModels.SingleOrDefault(v => v.CaseId == vid);

我收到错误:

  

未配置任何数据库提供程序。配置数据库提供程序   覆盖DbContext类或中的OnConfiguring   设置服务时的AddDbContext方法。

EF7 RC1中具有多个数据库上下文并访问它们的任何工作示例都将非常受欢迎。

1 个答案:

答案 0 :(得分:8)

首先,我会从GitHub上的EntityFramework的wiki推荐你the article。本文介绍了许多定义DbContext的方法,它引用了appsettings.json的一部分。我个人更喜欢使用[FromServices]属性的方式。

代码可以是以下内容:

首先,您使用以下内容定义了appsettings.json

{
  "Data": {
    "ApplicationDbConnectionString": "Server=(localdb)\\mssqllocaldb;Database=ApplicationDb;Trusted_Connection=True;MultipleActiveResultSets=true",
    "DecAppDbConnectionString": "Server=Server=(localdb)\\mssqllocaldb;Database=DecAppDb;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

您可以在其中定义两个连接字符串。

您宣布以DecAppContext为基类的类ApplicationDbContextDbContext。最简单的形式就是

public class ApplicationDbContext : DbContext
{
}
public class DecAppContext : DbContext
{
}

没有任何DbSet属性。

第三步。您使用Microsoft.Extensions.DependencyInjection注入数据库上下文。要做到这一点,您只需要在Startup.cs中加入类似

的内容
public class Startup
{
    // property for holding configuration
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();
        // save the configuration in Configuration property
        Configuration = builder.Build();
    }

    // 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()
            .AddJsonOptions(options => {
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            });
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options => {
                options.UseSqlServer(Configuration["Data:ApplicationDbConnectionString"]);
            })
            .AddDbContext<DecAppContext>(options => {
                options.UseSqlServer(Configuration["Data:DecAppDbConnectionString"]);
            });
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ...
    }
}

使用配置DbContextDecAppContext创建两个ApplicationDbContext"Data:DecAppDbConnectionString""Data:ApplicationDbConnectionString")。

现在我们可以在控制器中使用上下文。例如

[Route("api/[controller]")]
public class UsersController : Controller
{
    [FromServices]
    public ApplicationDbContext ApplicationDbContext { get; set; }

    [FromServices]
    public DecAppContext DecAppContext { get; set; }

    [HttpGet]
    public IEnumerable<object> Get() {
        var returnObject = new List<dynamic>();

        using (var cmd = ApplicationDbContext.Database.GetDbConnection().CreateCommand()) {
            cmd.CommandText = "SELECT Id, FirstName FROM dbo.Users";
            if (cmd.Connection.State != ConnectionState.Open)
                cmd.Connection.Open();

            var retObject = new List<dynamic>();
            using (var dataReader = cmd.ExecuteReader())
            {
                while (dataReader.Read())
                {
                    var dataRow = new ExpandoObject() as IDictionary<string, object>;
                    for (var iFiled = 0; iFiled < dataReader.FieldCount; iFiled++)
                        dataRow.Add(
                            dataReader.GetName(iFiled),
                            dataReader.IsDBNull(iFiled) ? null : dataReader[iFiled] // use null instead of {}
                        );

                    retObject.Add((ExpandoObject)dataRow);
                }
            }
            return retObject;
        }
    }
}

或使用async / await:

[Route("api/[controller]")]
public class UsersController : Controller
{
    [FromServices]
    public ApplicationDbContext ApplicationDbContext { get; set; }

    [FromServices]
    public DecAppContext DecAppContext { get; set; }

    [HttpGet]
    public async IEnumerable<object> Get() {
        var returnObject = new List<dynamic>();

        using (var cmd = ApplicationDbContext.Database.GetDbConnection().CreateCommand()) {
            cmd.CommandText = "SELECT Id, FirstName FROM dbo.Users";
            if (cmd.Connection.State != ConnectionState.Open)
                cmd.Connection.Open();

            var retObject = new List<dynamic>();
            using (var dataReader = await cmd.ExecuteReaderAsync())
            {
                while (await dataReader.ReadAsync())
                {
                    var dataRow = new ExpandoObject() as IDictionary<string, object>;
                    for (var iFiled = 0; iFiled < dataReader.FieldCount; iFiled++)
                        dataRow.Add(dataReader.GetName(iFiled), dataReader[iFiled]);

                    retObject.Add((ExpandoObject)dataRow);
                }
            }
            return retObject;
        }
    }
}

可以使用属性public ApplicationDbContext ApplicationDbContext { get; set; }声明属性[FromServices],ASP.NET会从ConfigureServices中注入的上下文中初始化它。同样,只要您需要,就可以使用第二个上下文DecAppContext

上面的代码示例将在数据库上下文中执行SELECT Id, FirstName From dbo.Users,并以[{"id":123, "firstName":"Oleg"},{"id":456, "firstName":"Xaxum"}]的形式返回JSON数据。由于Id中的使用FirstName,在序列化期间,属性名称从idfirstNameAddJsonOptionsConfigureServices的转换将自动完成。

更新:我必须参考the announcement。下一版本的MVC(RC2)将需要更改上面的代码以使用[FromServices]作为附加参数(例如方法Get())而不是使用公共属性[FromServices] public ApplicationDbContext ApplicationDbContext { get; set; }。需要删除属性ApplicationDbContext并向Get()方法添加其他参数:public async IEnumerable<object> Get([FromServices] ApplicationDbContext applicationDbContext) {...}。这样的改变很容易完成。请参阅here以及MVC演示示例中的更改示例:

[Route("api/[controller]")]
public class UsersController : Controller
{
    [HttpGet]
    public async IEnumerable<object> Get(
                     [FromServices] ApplicationDbContext applicationDbContext,
                     [FromServices] DecAppContext decAppContext)
    {
        var returnObject = new List<dynamic>();

        // ...  the same code as before, but using applicationDbContext 
        // and decAppContext parameters instead of ApplicationDbContext
        // and DecAppContext properties
    }