简体   繁体   中英

Is it possible to make entities from two different edmx's map to the same class in Entity Framework?

I've been asked to make a program I've written work with two different data providers: MS SQL and Oracle.

I've written two separate edmx files: one for the MS SQL database and one for the Oracle SQL database. Other than the fact that they use different data providers, the schema of the data is exactly the same.

I now have two entity sets and two entity classes.

Since the classes have the same properties, is it possible to use the same .net class for both entity sets?

I got the idea that this may be possible because as the system tried to generate two classes with the same name in different edmx files, it produced an error that said that the properties were defined more than once!

If your database schemas are exactly the same, you can work this out by using one Model with multiple connection strings configured.

To make the code more cleaner, you may inherit two context classes from the one generated for your model:

public class MSSQLContext : YourGeneratedContext 
{ 
    public MSSQLContext() 
        : base("MSSQLDatabase") /*MS SQL connection string name in your config file*/
    { 
    } 
}

and

public class OracleContext : YourGeneratedContext 
{ 
    public OracleContext() 
        : base("OracleDatabase") /*Oracle connection string name in your config file*/
    { 
    } 
}

Next, where needed you can create instance of MSSQLContext or OracleContext , you can work with the two at the same time as well..

using(OracleContext mainContext = new OracleContext())
{
    using(MSSQLContext mirrorContext = new MSSQLContext())
    {

    }
}

http://msdn.microsoft.com/en-us/data/jj592674.aspx

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