实体框架多重连接错误 [英] Entity Framework Multiple Connections Error

查看:96
本文介绍了实体框架多重连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,其中在appsettings.json下定义了多个连接字符串,如下所示:

I have a scenario wherein I have multiple connection strings defined under appsettings.json like this:

"ConnectionString": {
    "ConnectionZone1": "Server=(localdb)\\mssqllocaldb;Database=Blogging;Trusted_Connection=True;",
    "ConnectionZone2": "Server=localhost;Database=Blogging;Trusted_Connection=True;"
},

这也是我在startup.cs文件中注册的:

This I have registered in my startup.cs file as well:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<DbContextZone1>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("ConnectionZone1")));

        services.AddDbContext<DbContextZone2>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("ConnectionZone2")));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

我已经使用数据库优先方法创建了Model和context类,并按如下所示注册了我的上下文类:

I have created Model and context classes using database first approach, and registered my context classes as follows:

public partial class BloggingContext : DbContext
{
    public BloggingContext()
    {
    }

    public BloggingContext(DbContextOptions<BloggingContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Blog> Blog { get; set; }
    public virtual DbSet<Post> Post { get; set; }

并创建了另外两个上下文类,它们从上述主要基类继承:

and created two other context classes which inherits from the above main base class:

public class DbContextZone1 : BloggingContext
{
    public DbContextZone1()
    {
    }        
}

public class DbContextZone2 : BloggingContext
{
    public DbContextZone2()
    {

    }
}

现在,我已经创建了我的API控制器,并试图调用这些上下文方法.

Now I have created my API controllers and am trying to call these context methods.

[HttpGet]
    public async Task<ActionResult<IEnumerable<object>>> GetItems()
    {
        if (alternate)
        {
            alternate = false;
            using (var context = new DbContextZone1())
            {
                return await context.Blog.ToListAsync();
            }
        }

        using(var context = new DbContextZone2())
        {
            return await context.Post.ToListAsync();
        }            
    }

问题是,当我运行我的应用程序时,它抛出错误,我的上下文类应该具有参数化的构造函数才能传递选项.

The issue is when I run my application it throws error that my context class should have parameterized constructor in order to pass options.

因此在DbContextZone1和DbContextZone2构造函数中,哪个上下文选项参数将出现?我尝试过这样放置,但在调用API控制器时它永远不会起作用并抛出错误:

So in the DbContextZone1 and DbContextZone2 constructor which context options parameter will come?. I tried putting like this, but it never works and throws error when I call the API controller:

public class DbContextZone1 : BloggingContext
{
    public DbContextZone1(DbContextOptions<BloggingContext> options)
        : base(options)
    {
    }        
}

public class DbContextZone2 : BloggingContext
{
    public DbContextZone2(DbContextOptions<BloggingContext> options)
        : base(options)
    {

    }
}

这是错误:

那么,关于如何实现多个连接或使我的代码正确的任何帮助或代码建议或想法?.

So any help or code ideas or suggestions in how to achieve multiple connections or make my code right?.

推荐答案

从您的appsettings.json中,您似乎想连接到不同服务器中的同一数据库.您无需创建基本的DbContext,只需继承默认的DbContext如下所示:

From your appsettings.json,it seems that you want to connect to the same database in different server.You are no need to create a base DbContext,just inherits default DbContext like below:

public class DbContextZone1 : DbContext
{
    public DbContextZone1(DbContextOptions<DbContextZone1> options)
        : base(options)
    {
    }
    public virtual DbSet<Blog> Blog { get; set; }
}
public class DbContextZone2 :DbContext
{
    public DbContextZone2(DbContextOptions<DbContextZone2> options)
        : base(options)
    {
    }
    public virtual DbSet<Post> Post { get; set; }
}

并按如下所示调用API控制器:

And call the API Controller like below:

private readonly DbContextZone1 _context1;
private readonly DbContextZone2 _context2;
    public ABCController(DbContextZone1 context1, DbContextZone2 context2)
    {
        _context1 = context1;
        _context2 = context2;
    }
    [HttpGet]
    public async Task<ActionResult<IEnumerable<object>>> GetItems()
    {
        //....
        if (alternate)
        {
            alternate = false;
            return await _context1.Blog.ToListAsync();
        }
        return await _context2.Post.ToListAsync();
    }

这篇关于实体框架多重连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆