I created a database and generated it in essence, i.e. I have formed classes from this very database. Can I directly use these entities? or is it better to first create a separate class and assign values ​​from the entity already from this class?

    2 answers 2

    You can use these generated classes. They actually for this purpose are.

      Of course you can use it. But there is one nuance - they are generated automatically, and it is highly undesirable to change them. There is even a special warning about this in the files of each such model, which reads:

      This code was generated from a template. Unexpectedly in your application. If the code is regenerated, it will be overwritten.

      That is, if you suddenly need (and you will most likely sooner or later need) to somehow change / supplement them, then all such changes will be erased after the next update of the models from the database. This is pretty unpleasant.

      There are at least two possible ways out:

      The first. The generated entities are partial-classes, respectively, they can be extended using other files, which will describe the "missing" parts of your entity. Advantages: there is no need to start some other classes, but you can immediately use the existing Disadvantages: you can expand them in this way, of course, but it is difficult to change these entities. If you are not satisfied with something automatically generated properties in these classes that you would like to change or delete, then again have to face the danger of manually changing the automatically generated code.

      The second way. You can write your own models, which you will then somehow convert to the Entity Framework entity and vice versa. Advantages: In this case, your models can be more flexible, you can change them as you please at your own discretion and project the entities from EF onto these models as you like. Disadvantages: the need to create new classes and define the rules for converting these classes to the essence of EF and back

      • Ie it is advisable to create classes separately all the same? and then the properties of these classes assign the properties of the generated entities, I understand? - Ildarik07
      • @ Ildarik07 you can make your classes and use what kind of mapper to automatically convert back and forth - Bald
      • @Ildarik07 is not correct. It depends more on your preferences and on the situation in each specific case - DreamChild