在实体框架MVC中使用不同数据库的特定表/s

时间:2022-01-31 09:51:34

I have been searching on different threads but was unable to find an answer which can give me clear picture.

我一直在不同的思路上搜索,但是找不到一个能给我清晰画面的答案。

Here is the scenario:

这是场景:

I am creating an application using MVC & Entity framework DB first . I have two databases, DB1 is dedicated DB for my application and DB2 is a huge common Database getting used by many other applications. I will only have to use specific tables/views from DB2.

我首先使用MVC和Entity framework DB来创建一个应用程序。我有两个数据库,DB1是我的应用程序专用的DB, DB2是许多其他应用程序使用的大型公共数据库。我只需要使用DB2中的特定表/视图。

Now following are the approches I can go with:

下面是我可以采用的方法:

  1. Use normal SQL commands for DB2 and populate viewmodels with them. (I only have read-only access on DB2)
  2. 对DB2使用普通的SQL命令,并用它们填充viewmodel。(我只对DB2有只读访问)
  3. Create SQL views in the DB1 and generate EDMX.
  4. 在DB1中创建SQL视图并生成EDMX。
  5. Add DB2 in the context of my application.
  6. 在我的应用程序上下文中添加DB2。

I dont want to go with Approach#3 as I mentioned DB2 is huge and I only need to use couple of tables from DB2.

我不想采用方法#3,因为我提到DB2非常庞大,我只需要使用DB2中的几个表。

Also, Just in case I want to go with code first approach for DB1 what is the best solution in that case.

另外,为了以防我想要使用DB1的代码优先方法,在这种情况下最好的解决方案是什么。

1 个解决方案

#1


1  

I often have projects with three of more models because we have many databases.

我经常有三个模型的项目,因为我们有很多数据库。

I would create a two EDMX set-up with two distinct contexts. Create your application DB1 Context as normal then create an additional context and only pull in those tables that you are interested in from DB2.

我将创建一个带有两个不同上下文的EDMX设置。将您的应用程序DB1上下文创建为正常,然后创建一个附加上下文,并只提取您感兴趣的DB2表。

To make your life easier in the long run and easier to maintain generally just create a DLL for each model so that it has its own namespace and that way you can distinguish between users in DB1 and users in DB2 for instance and add or remove entities from one without affecting the other.

长期使你的生活更容易和更容易维护一般只会为每个模型创建一个DLL,它有自己的名称空间,这样你可以区分用户DB1和用户在DB2实例和添加或删除实体从一个不影响另一个。

Each DLL would have an app.config connection string that gets yo to your data, such as

每个DLL都有一个app.config连接字符串,该字符串可以将yo连接到您的数据,例如

<add name="DB1Entities" connectionString="metadata=res://*/DB1Model.csdl|res://*/DB1Model.ssdl|res://*/DB1Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=ClientDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /&gt;

<add name="DB2Entities" connectionString="metadata=res://*/DB2Model.csdl|res://*/DB2Model.ssdl|res://*/DB2Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=ClientMaster;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/&gt;

<add name="DefaultConnection" connectionString="Data Source=(local);Initial Catalog=Reports;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>

Just remember to copy each of the connection string from the Dll's App.config into your applications app.config or you web.config file for a site.

只需记住将Dll的App.config中的每个连接字符串复制到应用程序App.config或web中。一个站点的配置文件。

In your project reference the DLL's and then load your contexts.

在您的项目中引用DLL,然后加载上下文。

DB1Entities DB1Context = new DB1Entities()
DB2Entities DB2Context = new DB2Entities()

You can now happily distinguish between DB1 and DB2 entities and use content from one in the other like this.

现在您可以很高兴地区分DB1和DB2实体,并像这样使用内容。

var address1 =  DB1Context.Addresses.Single(a => a.AddressId == 1);
var address2 =  DB2Context.Addresses.Single(a => a.Id == address1.GlobalAddressId);

#1


1  

I often have projects with three of more models because we have many databases.

我经常有三个模型的项目,因为我们有很多数据库。

I would create a two EDMX set-up with two distinct contexts. Create your application DB1 Context as normal then create an additional context and only pull in those tables that you are interested in from DB2.

我将创建一个带有两个不同上下文的EDMX设置。将您的应用程序DB1上下文创建为正常,然后创建一个附加上下文,并只提取您感兴趣的DB2表。

To make your life easier in the long run and easier to maintain generally just create a DLL for each model so that it has its own namespace and that way you can distinguish between users in DB1 and users in DB2 for instance and add or remove entities from one without affecting the other.

长期使你的生活更容易和更容易维护一般只会为每个模型创建一个DLL,它有自己的名称空间,这样你可以区分用户DB1和用户在DB2实例和添加或删除实体从一个不影响另一个。

Each DLL would have an app.config connection string that gets yo to your data, such as

每个DLL都有一个app.config连接字符串,该字符串可以将yo连接到您的数据,例如

<add name="DB1Entities" connectionString="metadata=res://*/DB1Model.csdl|res://*/DB1Model.ssdl|res://*/DB1Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=ClientDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /&gt;

<add name="DB2Entities" connectionString="metadata=res://*/DB2Model.csdl|res://*/DB2Model.ssdl|res://*/DB2Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=ClientMaster;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/&gt;

<add name="DefaultConnection" connectionString="Data Source=(local);Initial Catalog=Reports;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>

Just remember to copy each of the connection string from the Dll's App.config into your applications app.config or you web.config file for a site.

只需记住将Dll的App.config中的每个连接字符串复制到应用程序App.config或web中。一个站点的配置文件。

In your project reference the DLL's and then load your contexts.

在您的项目中引用DLL,然后加载上下文。

DB1Entities DB1Context = new DB1Entities()
DB2Entities DB2Context = new DB2Entities()

You can now happily distinguish between DB1 and DB2 entities and use content from one in the other like this.

现在您可以很高兴地区分DB1和DB2实体,并像这样使用内容。

var address1 =  DB1Context.Addresses.Single(a => a.AddressId == 1);
var address2 =  DB2Context.Addresses.Single(a => a.Id == address1.GlobalAddressId);