上下文在代码优先模式与代码是从一个EDMX文件生成正在使用的 [英] The context is being used in Code First mode with code that was generated from an EDMX file

查看:2117
本文介绍了上下文在代码优先模式与代码是从一个EDMX文件生成正在使用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个MVC 5应用程序与EF 6数据库的第一种方法。现在,我希望我的DbContext访问基于不同用户的多个数据库。对于这个问题我生成的Web.config文件中的连接字符串为:

I am developing an MVC 5 application with EF 6 database first approach. Now I want my DbContext to access multiple databases based on different Users. For that matter I generated a connection string in Web.Config file as:

string str = u.Users.Where(x => x.UserName == HttpContext.User.Identity.Name).First().ConnStr;
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");          
ConnectionStringSettings c = new ConnectionStringSettings();    
c.ConnectionString = str;
c.Name = u.Users.Where(x => x.ConnStr == str).First().DbName;
c.ProviderName = "System.Data.SqlClient";
section.ConnectionStrings.Add(c);
configuration.Save();

在配置文件中生成的连接字符串是:

The connection string generated in config file is:

<add name="Awais123" connectionString="Data Source=.;Initial Catalog=Awais123;integrated security=True;MultipleActiveResultSets=True"
      providerName="System.Data.SqlClient" />

和由EF生成的,当我做EDMX连接字符串是:

And the Connection String generated by EF when I made EDMX is:

<add name="ABCEntities" connectionString="metadata=res://*/Models.awais.csdl|res://*/Models.awais.ssdl|res://*/Models.awais.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Awais;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
      providerName="System.Data.EntityClient" />



然后我重载我的实体构造函数:

Then I overloaded my Entities constructor as:

public ABCEntities(string nameorConnString) : base(nameorConnString) {}

和默认的构造函数是:

public ABCEntities() : base("name=ABCEntities") {}

现在我宣布在另一个cs文件另一个的DbContext类来生成数据库:

Now I declare another DbContext class in another .cs file to generate the database as:

public partial class NewDbGen : DbContext
{
     public NewDbGen(string nameorConnString): base(nameorConnString)
     { Database.Create(); }
}



我调用的DbContext对象为:

I am calling the DbContext objects as:

private static string str = u.Users.Where(x => x.UserName == HttpContext.User.Identity.Name).First().ConnStr;
private NewDbGen de = new NewDbGen(str); // for creating a new database
private ABCEntities db = new AbcEntities(str); // my original Entites.. and all my controllers use these entites for all queries

现在这一切都运行成功和数据库在SQL Server中创建的。可是后来,当我试图把该数据库发生错误的查询:

Now all this runs successfully and database is created in SQL Server. But later when I tried to query on that database an error occured in:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     throw new UnintentionalCodeFirstException();
}

和它抛出一个错误

上下文是在代码中第一个模式的代码,这是
从其中任何一个数据库首先或模型优先
发展的EDMX文件生成被使用。这将无法正常工作。要解决这个问题并不
删除代码,将引发此异常的行。如果您希望使用
数据库优先或模型优先,然后确保实体
框架的连接字符串包含在初创项目的app.config或
web.config中。如果您正在创建自己的
的DbConnection,然后确保它是一个EntityConnection,而不是
一些其他类型的DbConnection,那你把它传递给取
基的DbContext构造函数之一一个的DbConnection。要了解更多
关于代码首先,数据库优先和模型优先看到实体
框架文件的位置:
http://go.microsoft.com/fwlink/?LinkId=394715

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

我能做些什么?我必须使用单一的DbContext为所有用户数据库,并在控制器方法ABCEntities即DB访问数据库的同一个对象。
我已经看着的这个问题 DbFactory方法,但同样的错误发生。

What can I do? I have to use single DbContext for all User Databases and same object of ABCEntities i.e. "db" to access database in controller methods. I have already looked into this question DbFactory method, but the same error occured.

我想打一个单实例多租户应用程序

I want to make a Single Instance Multi-Tenant application

问候。

推荐答案

当您使用数据库,第一种方法,并且使用EDMX,然后用OnModelCreating是没有意义的。

When you are using Database-First approach and you use edmx, then using OnModelCreating makes no sense.

要拥有使用每个租户的策略数据库中的单个实例应用程序,你应该:

To have a single instance application that uses database per tenant strategy, you should:


  • 使用一个单一的DbContext类所有租户

  • 添加过载到你的DbContext构造接受connnectionstring

  • 创建为每个租户
  • $ b数据库$ b
  • 创建基于您的租户检测策略

  • 创建的DbContext实例传递你在运行时根据您的租户检测策略创建的ConnectionString在运行时的ConnectionString

  • Use a single dbcontext class for all tenants
  • Add an overload to your dbcontext constructor that accept connnectionstring
  • create a database for each tenant
  • create connectionstring at run-time based on your tenant detection strategy
  • create an instance of dbcontext passing the connectionstring that you created at run-time based on your tenant detection strategy

这篇关于上下文在代码优先模式与代码是从一个EDMX文件生成正在使用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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