title: Placing Entity Framework models in the infrastructure layer in onion architecture
body: <p>I have a web application being built using DDD and Onion architecture in .NET Core 5.</p>
  <p>There are 4 layers:</p>
  <ol>
  <li>Domain layer - which contains domain models</li>
  <li>Application layer - which has core business logic and have reference to domain layer</li>
  <li>Infrastructure layer - which uses EF Core to retrieve and save data to Azure SQL, and it refers to the application layer</li>
  <li>API layer - which has controllers and refers to application and infrastructure layers</li>
  </ol>
  <p>Reference to infrastructure layer via API layer is only to connect to the database and any request to database will go via application layer; the application layer has interfaces that are implemented in the infrastructure layer to save and retrieve data from the database.</p>
  <p>Now, when I create database using a code-first or database-first approach, where should these EF model classes should go?</p>
  <p>Placing them in the domain layer will break the onion architecture, as the domain layer should not know anything about the infrastructure layer.</p>
  <ol>
  <li>Placing models and mappings in Domain layer</li>
  </ol>
  <p>I have seen couple of examples where models and mappings are placed in domain model and those mappings are used in infrastructure layer <code>OnModelCreating(..)</code>, but still the infrastructure layer mappings which are database-specific are present in the domain layer. What if I want to replace the infrastructure layer with some other database, in which case I need to change the domain model which seems not right to me..</p>
  <ol start="2">
  <li>Placing models in infrastructure layer</li>
  </ol>
  <p>I can place EF model classes in the infrastructure layer, but how do I reference them in the application layer? Do I need to have an interface for each model repository or is there any multiple dependency injection for same interface?</p>
  <p>I want domain model to hold by business models and place EF models in infrastructure layer and use AutoMapper to map between them if necessary.</p>
  <p>So what is the best practice to hold EF models so it won't break Onion architecture.</p>