简体   繁体   中英

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. 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. 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.

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.

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"*

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. 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.

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.

You are going to use model first approach as the database already exists. You will need to create two edmx for the two database. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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