【问题标题】:ASP WEB API multiple DBContextASP WEB API 多个 DBContext
【发布时间】:2016-10-04 06:45:48
【问题描述】:

我有带有 2 个控制器的 app asp.net core web api app OracleController.cs

        [Route("api/[controller]")]
        public class OracleController : Controller
        {
            private readonly OracleDbContext _db;  
            public HeatsController(OracleDbContext context)
            {
                _db = context;
            }
            ...
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    _db.Dispose();
                }
                base.Dispose(disposing);
            }
    }

SqlServerController.cs

        [Route("api/[controller]")]
        public class SqlServerController: Controller
        {
            private readonly SqlServerDbContext _db;  
            public HeatsController(SqlServerDbContext context)
            {
                _db = context;
            }
            ...
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    _db.Dispose();
                }
                base.Dispose(disposing);
            }
    }

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped(_ => new OracleDbContext(Configuration["Data:OracleConnectionString"]));
            services.AddScoped(_ => new SqlServerDbContext(Configuration["Data:SqlServerConnectionString"]));
        }

SqlServerDBContext.cs

public class SqlServerConfig : DbConfiguration
    {
        public SqlServerConfig()
        {
            SetProviderServices("System.Data.SqlClient",
             System.Data.Entity.SqlServer.SqlProviderServices.Instance);
        }
    }

    [DbConfigurationType(typeof(SqlServerConfig))]
    public class SqlServerDbContext : DbContext
    {
        public SqlServerDbContext()
            : base("name=SqlServerDbContext")
        {
        }

        public SqlServerDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {
        }

        ...

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            ...
        }
    }

应用启动后,我只能访问一个 DBContext。例如,如果我从 OracleController 调用某些方法,它工作正常,但来自 SqlServerController 的方法不起作用,它们会抛出异常“提供的 SqlConnection 未指定初始目录或 AttachDBFileName。”。如果我先从 SqlServerController 调用方法,它的工作方式相同。

【问题讨论】:

  • 你的数据库上下文有一个基本的 DbContext 吗?我的意思是父类。
  • 都继承自 DbContext;公共类 OracleDbContext : DbContext { ... }
  • 好的,你没有自定义父类。我认为您需要查看您的 ConfigureServices 方法,我有这个用于配置 Sql Server DbContext: services.AddEntityFrameworkSqlServer().AddDbContext();
  • 我使用的是 EF 7(核心),但我使用的是 EF 6。我不清楚你的意思是什么
  • 在 EF6 中,您需要在 web.config 文件中为每个连接字符串设置 providerName,您检查过它们吗?

标签: c# oracle entity-framework-6 asp.net-core-webapi


【解决方案1】:

正如您在错误消息中看到的,您的连接字符串中缺少InitialCatalog 属性。这是指定 EntityFramework 应连接到哪个数据库并针对其启动查询所必需的。

【讨论】:

  • 我的连接字符串中有 InitialCatalog
  • 你能告诉我你正在使用的连接字符串吗?
  • 数据源=****;初始目录=****;用户ID=****;密码=****;MultipleActiveResultSets=True;App=EntityFramework;
  • 两个数据库是否使用相同的连接字符串?
  • 不,我使用不同的连接字符串
猜你喜欢
  • 2019-06-29
  • 1970-01-01
  • 2018-07-26
  • 2019-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多