Hello.

I have the following situation. There are a large number of entities in the project (21 pcs.) How is it necessary from the point of view of architecture and generally correctness to work with them? Create one context for all 21? Or try to somehow logically divide the entities into groups and for each make their own context?

    1 answer 1

    Despite the fact that if the business model is successfully split into several contexts, you can gain an advantage in readability and maintainability of the code, you can also face unforeseen difficulties in implementing such simple things as:

    1. Creating a database using the code first approach. Only one context will be able to create a new database, and therefore there will be no tables for other contexts in this database.

    2. Manage relationships between objects. Two related classes cannot be placed in different contexts. Consequently, in the business model there should be no related groups of classes so that they can be divided into contexts.

    Added by

    This blog describes in detail what needs to be done so that EF 6 can work normally in such a scenario: Data Points - EF6 Code First Migrations for Multiple Models .

    • Thank you very much, I still today faced a strange phenomenon that you described in A.2. There are 2 repositories: CarMakeRepo, CarModelRepo, Each of them creates its own context instance (from only 1 with all DbSets). When adding through the first data repository, I can’t add related data after 2. - Bogdan Stefanjuk
    • In more detail on screenshots, here is a reference - Bogdan Stefanjuk