Combine Code First & Database First In Single Model?

entity-framework entity-framework-6


Is there a way to combine code-first and database-first in the same context? We are running into massive development-time performance problems when editing the EDMX file (it takes 1.5 minutes to save). I've moved our non-insert/update/delete UDFs/stored procs to some custom T4 templates that automatically generate model-first code, but I can't seem to get OnModelCreating to be called when EDMX is involved.

Other things we've considered, but won't work for one reason or another:

  1. We can't (reasonably) separate our code to multiple contexts as there is a lot of overlap in our entity relationships. It also seems like quite a people who have gone this route regret it.

  2. We tried having 2 different contexts, but there are quite a few joins between Entities & UDFs. This may be our last hope, but I'd REALLY like to avoid it.

  3. We can't switch to Dapper since we have unfortunately made heavy use of IQueryable.

  4. We tried to go completely to Code-First, but there are features that we are using in EDMX that aren't supported (mostly related to insert/update/delete stored procedure mapping).

11/9/2016 5:43:12 PM

Accepted Answer

Thank you to everyone for the well thought out and thorough answers.

Many of these other answers assume that the stored procedure mappings in EF Code-First work the same, but they do not. I'm a bit fuzzy on this as it's been about 6 months since I looked at it, but I believe as of EF 6.3 code first stored procedures require that you pass every column from your entity to your insert/update stored procedure and that you only pass the key column(s) to your delete procedure. There isn't an option to pick and choose which columns you can pass. We have a requirement to maintain who deleted a record so we have to pass some additional information besides just a simple key.

That being said, what I ended up doing was using a T4 template to automatically generate my EDMX/Context/Model files from the database (with some additional meta-data). This took our developer time experience down from 1.5 minutes to about 5 seconds.

My hope is EF stored procedure mappings will be improved to achieve parody with EDMX and I can then just code-generate the Code-First mappings and remove the EDMX generation completely.

11/15/2016 9:13:10 PM

Popular Answer

Take a look at the following link. I answered another question in a similar fashion:
How to use Repository pattern using Database first approach in entity framework

As I mentioned in that post, I would personally try to switch to a Code First approach and get rid of the EDMX files as it is already deprecated and most importantly, the maintenance effort is considerable and much more complex compared with the Code First approach.

It is not that hard switching to Code First from a Model First approach. Some steps and images down below:

  1. Display all files at the project level and expand the EDMX file. You will notice that the EDMX file has a .TT file which will have several files nested, the Model Context and POCO clases between them as .cs or .vb classes (depending on the language you are using). See image down below:
    enter image description here
  2. Unload the project, right click and then edit.
  3. See the image below, notice the dependencies between the context and the TT file
    enter image description here
  4. Remove the dependencies, the xml element should look like the image below:
    enter image description here
  5. Repeat the procedure for the Model classes (The ones with the model definition)
  6. Reload your project, remove the EDMX file(s)
  7. You will probably need to do some tweeks and update names/references.

I did this a few times in the past and it worked flawlessly on production. You can also look for tools that do this conversion for you.

This might be a good opportunity for you to rethink the architecture as well.

BTW: Bullet point 4 shouldn't be a show stopper for you. You can map/use Stored Procedures via EF. Look at the following link:
How to call Stored Procedure in Entity Framework 6 (Code-First)?

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