With Entity Framework Core database first approach how do I separate my entity from infrastructure layer?

asp.net-core-webapi c# clean-architecture entity-framework-core

Question

I'm building REST APIs with ASP.NET Core and Entity Framework Core with a database-first approach with clean architecture.

My database is already created and maintained by a DBA team - that's why I need to use Entity Framework Core with a database-first approach, and we need to do reverse engineering using the Scaffold-DbContext command in the infrastructure layer.

With clean architecture, entities should be placed in Core layer separate from the infrastructure layer so how we will do that and how to resolve your DbContext as an interface.

1
0
2/21/2020 9:21:15 AM

Accepted Answer

I see two options:

  1. You accept the dependency to the EF, put the generated entities into your core project and so accept the violation to clean architecture.

  2. You create your own entities in Core project without dependencies to EF and use repository pattern to "map" between your entities and those from EF.

Alternative 1 is simple and cheap. Alternative 2 gives you independence from EF and some abstraction to your dba team. Choose based on what is more important for your project.

0
2/27/2020 6:40:22 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow