My Question is can we extend one context to another in Entity Framework 4. Following is the problem background.
I am using EF4 for developing a Web-Application. My Web Application has 3 projects.
- One project is for storing candidate CV information.
- Another project is for storing customer information.
- Final project is called CORE. this CORE contains a edmx file that should be extended by CV and CRM.
As an example email address table can be shared by both customer (CRM) and CV. To share one table with another, we should 开发者_如何学运维be able to extend one context (CV context) with other context (customer context). Could some one having experience using Entity Framework share your experience with me to find whether this is possible.
We have 3 edmx files in three class libraries:
- Person class library(Core) with core.edmx-Basic details related to a person(name, email)
- CV Class library with CV.edmx wich have CV related data(Experiance,skills)
- Customer Relationship management system(CRM) which have customer data(sales data...)
Then we want to use core class library edmx file(Core.edmx) with cv.edmx (basically cv tables should be able to use tables from core tables) . Same way we want to use the core.edmx with CRM.edmx for the 3 rd project
Well first of all, your EDMX's shouldn't (actually can't) talk to each other.
It's usually a 1-1 between Context -> EDMX -> Database.
A better solution would be encapsulate your Model's behind a Repository.
One Repository per aggregate root will give you a PersonRepository, CvRepository, CustomerRepository.
Each DLL can expose a single Repository.
Then, you would need a "Service Layer", which can work with the repositories. Similar to a "Unit of Work".
E.g you might create a CvPersonService, which could handle interactions with both the Person (Core) and CV Repositories:
using PersonRepository;
using CvRepository;
public class CvPersonService
{
public CvPerson GetHydratedCvPersonFromTwoRepositories()
{
var person = personRepository.Find(1); // get a person
var cv = cvRepository.Find(1); // get a cv
return new CvPerson { ThePerson = person, TheCv = cv };
}
}
The important thing to note is, EF makes no assumptions about database connections and the context. A database associated with a particular context could be on a local machine, a development server, or on the cloud. EF doesn't know (or care) about WHERE the database is. The SQLProvider in your configuration specific this. EF must assume it could be anywhere.
Therefore, you can't really "JOIN" queries from one context to another. How would this work? Maybe in "old-school", you could use a linked server, but it's not feasible for an ORM like this.
Put it this way:
- Create a database hosted on your local PC (DB1)
- Create a database hosted on the internet. (DB2)
- Write a query to join results from two tables, one from DB1, one from DB2.
You can't do it can you? Neither can Entity Framework. :)
Even if all three EDMX/Context's used the same database, it wouldn't matter.
The above would be a better solution - rearchitect it in a way which you can gracefully expose the models via fluent Repositories.
HTH
精彩评论