Combine Code First & Database First In Single Model?

entity-framework entity-framework-6


Is it possible to mix code-first and database-first approaches in one project? When modifying the EDMX file, we are seeing severe performance issues throughout development (it takes 1.5 minutes to save). Our non-insert/update/delete UDFs and stored procs have been transferred to some unique T4 templates that spawn model-first code automatically, but I still can't seem to make them to work.OnModelCreating whenever EDMX is involved, to be called.

Other options that we thought about but ultimately decided upon include:

  1. Our entity connections have a lot of overlap, thus we can't (fairly) split our code for different circumstances. Many of the individuals who have chosen this path seem to have changed their minds.

  2. Although we attempted to use two distinct contexts, there are several joins between Entities & UDFs. Even though that could be our final option, I'd rather want to avoid it.

  3. Dapper is not an option for us since we have, regrettably, used it extensively.IQueryable .

  4. Although we made an effort to switch entirely to Code-First, several of the EDMX capabilities we use—most notably the insert/update/delete stored procedure mapping—are not supported.

11/9/2016 5:43:12 PM

Accepted Answer

11/15/2016 9:13:10 PM

Popular Answer

Visit the following website to learn more. I provided a similar response to another question:
How to implement the repository pattern using the entity framework's database first approach

As I said in that piece, I would personally attempt to abandon the EDMX files in favour of a Code First approach since they are already deprecated and, more significantly, because maintaining them requires a significant amount of work and is much more difficult than with a Code First strategy.

Changing from a Model First approach to a Code First one is not that difficult. Steps and pictures are provided below:

  1. Expand the EDMX file, and then see all files at the project level. You'll see that the EDMX file has a.TT file that contains multiple nested files, each with the Model Context and POCO clases as.cs or.vb classes (depending on the language you are using). See illustration below:
    enter image description here
  2. Right-click the project to unload it, then choose Edit.
  3. Observe the dependencies between the context and the TT file in the illustration below.
    enter image description here
  4. Once the dependencies have been removed, the xml element should appear as seen below:
    enter image description here
  5. The Model classes should follow the same process (The ones with the model definition)
  6. your project once again, then delete the EDMX file (s)
  7. You should definitely make some adjustments and update names and references.

I've done this a few times before, and every time it produced excellent results. Search for tools that can do this conversion for you as well.

You may want to use this chance to reconsider the architecture as well.

By the way, you shouldn't allow bullet point 4 stop you. Using EF, you may map and utilise stored procedures. Visit the pursuing link:
How can I use Entity Framework 6's Code-First method to invoke a stored procedure?

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