I think Miguel led me to a solution:
Solution 1
In my case I had a separate project in my solution which keeps the data model. In that project I added another ADO.NET Entity Data Model generated from the MS SQL DB. At this point it would already work, besides that we would have to change the code wherever we create or access the context. To solve this I've created a interface
for the context object that the EF Wizard generated, and implemented it in a partial
type. This way I can "Update the Model from Database" without loosing it:
public partial class EntitiesMs : DbContext
{
public EntitiesMs()
: base("name=EntitiesMs")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
// Your DbSet<...> Stuff
}
public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
// Your DbSet<...> Stuff
}
And in separate files of course:
public partial class Entities : IDbEntities
{
}
public partial class EntitiesMs : IDbEntities
{
}
A Factory could then return the desired object. Although this has some downsides:
- If a new table is created in the DB the interface has to be modified
- You would have to
cast
the object from the factory to access
methods like SaveChanges()
(methods from the base type)
And maybe some others I have overlooked.
Solution 2
Another "quick and dirty" way I've found is to add another project to the solution and add the MS Data Model there. Make sure that the Model has the exact same name as the one for MySQL. Then the only things you have to do in your startup project is, switch the reference to the other project and switch the connection strings.