Issues to subdivide an entity framework model

The other day, my team and I ran into some design issues while trying to split a big Entity Framework model into smaller pieces according to areas of functionality. At first glance, it seemed to be a common design problem, something easy to overcome, but it did not result that way. I could not find much information about people having the same issue either.

In the system we are currently designing, we use modules or packages to group classes that belong to the same area of functionality. As you can guess, these modules do not represent more than a set of use cases or stories that are tightly coupled in design time.

We also have, however, some classes that are common for all those modules, so we put them in a shared module. In few words, we have a entity model shaped like a star, being the shared module the center of the star, and the rest of the modules just leafs.

The problem appeared when we tried to assign a different entity framework model to any of these modules for persisting the classes in a store. We could not find an easy way to reference the model with the shared entities from the rest of them, so we gave up after a while.

A single model with all the entities was not an option for us, because it simple does not scale up. We took a similar approach in the past with another project, the result was a huge model full of entities and really hard to maintain. This solution also required having a extra service layer on top of the entity model to encapsulate functionality specific to each module, which made very difficult to have a rich domain model at first glance.

After a search in several blogs, I came across this post "Working with large models in entity framework" from the Ado team, where they discuss some possible techniques or workarounds for an scenario like this.

They basically proposed the following solutions,

1. Reference one conceptual model (CSDL) from another. You create different models, and manually modify each CSDL to reference the model (CSDL) with the shared entities.

The CSDL model supports a clause "using" for including types defined in another model, it is basically a simple way to reuse types. This sounds good, however, it lacks of design time experience, which makes this feature totally useless for the scenario we have (and really complicated to implement according to this post written by Julie Lerman)

2. Duplicate metadata for the shared entities in each conceptual model (CSDL). Again, this approach sucks from a point of view of maintainability, typically you will have the same problems that you would see with duplicated code. One change in an shared entity will require changes in every model that reference it.

3. Expose foreign keys as scalar properties because you do not want to pull in all the shared entities into every Entity model. Not a good solution either, the shared entities get out of the unit of work or context (ObjectContext) generated by the designer. As result, you can not execute queries that require joins between the two contexts. Unless you include some helper methods to retrieve the shared entities using the scalar properties, you also lost the possibility of having a rich domain model.

As you can see, we could not find any workaround that totally met our expectations, so we might stick to the solution #2 (Duplicating the entities). The solution #1 would be perfect with some design time support, I hope the ado team consider this for a future release.

Do you have any thought or experience to share ?. I would be more than happy to hear your feedback regarding possible solutions for this scenario.


  • Hi Pablo,

    I had the same issue. And I was very dissapointed too.
    Workarounds are not good enough, so finally my conclusion was more or less the same as yours.
    I usually work on large-scale initiatives so modular design is a must and I pay special attention to that when trying new dev tools/frameworks....

    I tried to contact the product group about this issue, and it seems EF v2 will provide some support to modular design.

  • Late to the party on this one.

    I've come across the same issue and, even though I believe EF is not at version 1 anymore, I can't seem to find a way to implement a modular design either.
    Add to this the fact that I'm new to EF, silverlight and C3 in general, and have a tight deadline, and you can see why I don't have much hair left.

    If anybody has some insight on this, it would be more than welcome!

Comments have been disabled for this content.