简体   繁体   English

具有两个数据库的实体框架(完全不同的架构)

[英]entity framework with two databases (completely different schemas )

I am very new to entity framework and I am trying to do something like following scenario. 我对实体框架非常陌生,并且正在尝试执行以下方案。

I am creating ASP.net web from application. 我正在应用程序创建ASP.net Web That website needs to connect to two databases which the schemas are completely different. 该网站需要连接到两个架构完全不同的数据库。

I have no idea how the basic structure should be. 我不知道基本结构应该如何。

I though of have EF on class library. 我虽然在类库上有EF please guide me with instructions since I have less knowledge. 由于我所掌握的知识较少,请指导我。

Since you are using two different databases, the only viable option is to create two separate conceptual models. 由于您使用的是两个不同的数据库,因此唯一可行的选择是创建两个单独的概念模型。 Even if you would be able to merge two different databases into a single conceptual model, it would be a pain to maintain is the databases are of mentionable sizes. 即使您能够将两个不同的数据库合并为一个概念模型,也很难维护,因为数据库的大小是可以提及的。

The two models could reside within the same project, in seprate folders to get different namespaces. 这两个模型可以驻留在同一项目中的单独文件夹中,以获取不同的名称空间。

Eg: 例如:

Company.MyApp.DataModel
Company.MyApp.DataModel.Model1
Company.MyApp.DataModel.Model2

Then you could put a new layer on top of these two models which do all the heavy lifting, and could even make them look like one database if you want that, or merge data from entities in both models into a DTO or something similar. 然后,您可以在这两个模型的上面放置一个新层,以完成所有繁重的工作,如果需要,甚至可以使它们看起来像一个数据库,或者将两个模型中的实体的数据合并到DTO或类似的东西中。

Check out the Repository pattern . 查看存储库模式

If you think about it, when you create a EDM model with Visual Studio it ask you to give an existing database, and when finished creating the model, it generates an EF connection string, that internally address to the given underlying database connection string. 如果您考虑一下,当使用Visual Studio创建EDM模型时,它会要求您提供一个现有数据库,完成创建模型后,它将生成一个EF连接字符串,该字符串在内部寻址到给定的基础数据库连接字符串。

Eg: metadata=res:///EFTestModel.csdl|res:///EFTestModel.ssdl|res:///EFTestModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\\;Initial Catalog=EFTest;Integrated Security=True;MultipleActiveResultSets=True"* 例如:metadata = res:///EFTestModel.csdl | res:////EFTestModel.ssdl | res:///EFTestModel.msl; provider = System.Data.SqlClient; provider连接字符串=“ Data Source =。\\;初始目录= EFTest;集成安全性= True; MultipleActiveResultSets = True“ *

So each model matches only a database, only a connection string. 因此,每个模型仅匹配一个数据库,仅匹配一个连接字符串。

EF4 still does not support creating one conceptual model which works with N storage models. EF4仍然不支持创建一个可与N个存储模型一起使用的概念模型。 At least this is not supported with any built-in provider. 至少任何内置提供程序都不支持此功能。 Perhaps in the future this could be done through a new provider that combines the support of many storages (from the same providers or different). 也许将来可以通过新的提供程序来完成,该提供程序结合了许多存储(来自相同提供程序或不同提供程序)的支持。

I havent done enough research on it, but perhaps Windows Server AppFabric (Codename Velocity) could be the bridge to go through this gap. 我尚未对此进行足够的研究,但也许Windows Server AppFabric(代号Velocity)可能是克服这一差距的桥梁。

Note: I have tried even editing manually the xml for the EDM (edmx) to insert a second element inside the tag but it does not match the EDM XML Schema so VS warns about it: Error 10021: Duplicated Schema element encountered. 注意:我什至尝试手动编辑EDM(edmx)的xml以在标记内插入第二个元素,但它与EDM XML Schema不匹配,因此VS警告:错误10021:遇到重复的Schema元素。

You are going to use model first approach as the database already exists. 由于数据库已经存在,因此您将使用模型优先方法。 You will need to create two edmx for the two database. 您将需要为两个数据库创建两个edmx。 You can create one model project which you can have connection strings for both the edmx in the app.config file and then create your edmx. 您可以创建一个模型项目,在app.config文件中可以为edmx拥有连接字符串,然后创建edmx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM