简体   繁体   中英

Using different databases with Entity Framework 6

I have a problem with using different databases in a mvc application with Entity Framework 6. Our client will use a database of their own which could be a MySQL, PostgreSQL or Oracle database.

I have made an .edmx file for MySQL and PostgreSQL, these models work individually but if possible I want to use only one model for all databases. The databases will have identical tables and columns.

So want to make something like this:

using (var connection = new DbConnection())
{
    string id = connection.Set<user>().First().Id;
}

The DbConnection has to be a connection to the right database (you can see which database to use in a resource file)

I am encountering the following problem with a database first approach:

I have read http://www.codeproject.com/Articles/82017/Preparing-an-Entity-Framework-model-for-multi-prov.aspx and when I followed the instructions I got a

MetadataException: Unable to load the specified metadata resource exception.

I have tried everything to find the .ssdl file but I just can't find it.

Am I doing it the right way or does anyone know if there is a better way to do this?

I tried doing exactly what you are trying to do but with MS SQL and Vista DB. I followed that tutorial and ran into the same problem.

By following the tutorial, you will have a separate ssdl file for each DB. Make sure you have set it to copy to output directory and then updated your connection string accordingly.

To set it to copy to the output folder, right click on the ssdl file in Solution Explorer and change "Copy to OUtput Directory" to "Copy if Newer".

Then change the metadata part of your connection string to something like this:

metadata=res://*/DataModel.csdl|c:\dev\program\bin\debug\DataModel.VistaDB.ssdl|res://*/DataModel.msl;

Notice the path to the ssdl is a folder path. The path I have hear is the path I used. You will need to change it accordingly

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