Separation of Layers Design

Kmcnet 1,296 Reputation points
2026-02-03T21:39:40.1733333+00:00

Hello everyone and thanks for the help in advance. I am refactoring a MVC application that has become very disorganized due to poor separation of models, data access, views, etc. I decided to place the models in a separate solution, however, found that many of the models use a {Keyless} annotation, thus requiring Entity Framework to be added to the project. It was my intention to have a separate data access project that would handle the EF, however, this now appears to be impractical. Could someone give me input on how "real-world" applications handle this? Any insight would be appreciated.

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
{count} votes

Answer accepted by question author
  1. AgaveJoe 30,971 Reputation points
    2026-02-04T18:25:46.09+00:00

    Instead of using the [Keyless] attribute in the model project, keep your classes as POCOs and configure the 'keyless' behavior using the Fluent API within your OnModelCreating method in the Data Access project. This removes the need for a Core project to reference EF at all


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-02-03T21:39:52.6166667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    When refactoring your MVC application for better separation of concerns, it's essential to structure your projects effectively. In a typical real-world scenario, applications often adopt a layered architecture, which separates the application into distinct layers for presentation, business logic, and data access. Here are some insights on how to handle your situation:

    1. Layer Separation: It's common to have separate projects for each layer. For instance, you can have a project for your models (ApplicationCore), a project for data access (Infrastructure), and a project for the UI (ASP.NET Core UI). This separation helps maintain a clean architecture and allows for better organization of code.
    2. Keyless Entities: If your models are using the {Keyless} annotation, it indicates that these models do not have a primary key. This is often used for views or queries that return data but do not correspond to a specific entity in the database. In your data access project, you can still use Entity Framework Core to handle these models, but ensure that your data access layer is responsible for managing the context and any related database operations.
    3. Dependency Management: To avoid tight coupling between your projects, consider using interfaces for your data access methods defined in the ApplicationCore project. The Infrastructure project can then implement these interfaces. This way, your UI project does not need to directly reference Entity Framework or the Infrastructure project, promoting a clean separation.
    4. Using Dependency Injection: Implementing a dependency injection (DI) container can help manage dependencies between your layers. This allows you to inject the necessary services into your controllers without creating direct dependencies, which can lead to a more modular and testable architecture.
    5. Testing: Ensure that you also have separate test projects for each layer. This allows you to test the functionality of your models, data access, and UI independently, which is crucial for maintaining code quality as your application grows.

    By following these practices, you can create a well-structured application that adheres to the principles of separation of concerns, making it easier to maintain and scale in the future.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.