views:

809

answers:

1

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?

+2  A: 

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.

YeahStu