Large Entity Framework Models

These are my research notes on coping with large entity framework models.

The current community consensus appears to be that the designer will slow up and become less to unusable around the 200+ entity mark.

Additionally a single model will not allow you to work across databases and is complicated to work with.

So given your company/customer prefers a model(over code first), what options do you have?

Btw, these are my preferred ones, they are not exclusive and when I talk about models I’m referring to EDMX files.

  • Separate models sharing a single context.

    Hendry Ten shows you how you might automated the process of merging the MSL/CSDL/SSDL XML across models, you can only download the code on the last page of his blog.Note, that you can turn off the automated code gen and run a custom t4 template to give you more control over the boundaries e.g exclude generating class code for common classes included in multiple models.

  • Working across database boundaries

    Rachel Lim points out that you can do this by tricking EF if your database supports synonyms.

  • Multiple models each with there own context

    Rune Gulbrandsen has a handy technique if you just want to read some data from another context.Cheats aside you can use the unit of work pattern in a service above the data access/repository to orchestrate transactions between contexts. Slauma gives an example how this might be done here.Having the same ‘common’ entities like reference data in multiple models should only be a problem if are using a custom code generation script and then you can add some rules to stop the duplicate class outputs.

  • Seperate schema files
  • If you just want to seperate out the schema in your .edmx files take a look at the MultiFileEDMX project.

The issues may well be fixed in later releases of Entity Framework, but nothing will fixed large model complexity.

I prefer working with small models say less than 20 entities.

When working with smaller models it is easier to see the domain themes and the model names are descriptive in the solution like ‘Sales’. 

It is what Eric Evans describes when he explains aggregate roots, Ward Bell leads nicely into this is his blog coping with large models.

That’s it so far…

Extras

Perry Marchant wrote a good article on performance and entity framework.

CSDL – Conceptual schema definition language is an XML-based language that describes the entities, relationships, and functions that make up a conceptual model of a data-driven application.

SSDL Store schema definition language is an XML-based language that describes the storage model of an Entity Framework application.

MSL Mapping specification language is an XML-based language that describes the mapping between the conceptual model and storage model of an Entity Framework application.