I have three projects that share the same DB, whats the best way to use EF across all?

entity-framework project-planning


How will I use EF with these three projects (WCF projects, not customers) and a single database? Should I create a fourth project that contains the entities and the database context, then add references to it in the other three projects? Or should I just create a different context for every project and add the tables I require for each one? There are several tables that are actually utilized worldwide. So what's the best course of action here?

Should I make the EF database context accessible to other projects in a separate project? a phrase like:

 MySeparateProject myPr = new MySeparateProject();
 using (var db = new myPr.DBContext())
     // do stuff with entities
6/15/2013 12:23:12 AM

Accepted Answer

Making a data access project (class library) that only contains your models and database context and referencing it from all of your other projects seems to me to be the clearest course of action.

Some individuals will advise you to create a class library that contains only the models, followed by another class library that contains the DbContext and repository classes. This, in my opinion, is excessive for the bulk of tasks. It is incredibly simple to maintain all the dependent projects continuously in sync with regard to data access when the models and context are all in one location.

A typical project structure for me is as follows:

enter image description here

Here, the two "applications" (Squelch.Portal and CdrImport), as well as the different test cases, all make use of these base class libraries. Zzz-20-zzz contains all of my models and database contexts, while Squelch.Core contains fundamental business logic.

6/15/2013 1:10:33 AM

Popular Answer

I would start a different project for data access. Anyhow, it is best practice to isolate your data layer. Depending on the project's characteristics and the testing strategy you intend to use, you might want to look at something like the repository pattern (though there is debate about its value with EF).


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow