Multiple back ends for Entity Framework

Can I use two different back ends for an .net Entity Framework project?

I need to support the full programmability support of stored procedures on SQL server when available. I need to support only table structures in a .mdb file when SQL server is not available.

All business logic above the Entity Framework uses the entity abstratctions rather than going directly to the database. The entity model can use either stored procedure calls or dynamic sql to read/write to the database tables.

Is it possible to create two logically identical entity models, each with different mappings to the database (one managed by the framework, one managed by stored procedures), and switch between them at runtime based on the functionality provided by the back-end storage mechanism?


You should see this similar SO question. I am not sure if you can do this at runtime, but I've found this to be possible after deployment. Beware, there are definitely pitfalls.

The main differences between EDMX files that are generated between different database backends are the MSL and the SSDL. What I've done is generated the EDMX file separately from each database. They both have the same logical data model (CSDL). I then extract the MSL and SSDL file and save it into separate files. Once that is done, you can specify in your connection string the exact location of these files (as shown):

<add name="DBConnection" connectionString="metadata=C:\sqlServerEntities.csdl|C:\sqlServerEntities.ssdl|C:\sqlServerEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=[machinename];Initial Catalog=[databasename];Persist Security Info=True;User ID=[user];Password=[password];MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

<add name="DBConnection" connectionString="metadata=C:\mdbEntities.csdl|C:\mdbEntities.ssdl|C:\mdbEntities.msl;provider=[mdb provider namespace];provider connection string=[DB connection string]" providerName="System.Data.EntityClient" />

You'd have to use the appropriate connection string at runtime based on which database you're connecting to. I am concerned that you might have difficulty using STPs in one scenario and table-based mapping in the other though.

One other note: you cannot leave both the EDMX models in your project or you will create compiler errors (based on duplicate class definitions). However, you have to leave one in your project so the compiler knows about the logical classes that were generated.

Need Your Help

autoscrolling for UIScrollView

iphone iphone-sdk-3.0 uiscrollview

I used the setContentOffset method to scroll to a particular point automatically without user interaction.

Find Index In An Array Range

javascript jquery arrays sorting

I have an array of quantities and I would like to find where in this array a number is found. So if a user entered 16 the function would return between index 6 &amp; 7 because 16 is greater than 15...