Clean solution (project) structure with EF, Repositories, Entities

后端 未结 2 1823
生来不讨喜
生来不讨喜 2021-02-01 10:07

I like to keep the structure of the project as clean as possible. Sample:

--BlogApp.sln
  --BlogApp.Data
      BlogModel.edmx (the EF mappings)
      Post.cs (I         


        
2条回答
  •  无人及你
    2021-02-01 11:00

    Check out this write up on T4 Templates and the Entity Framework. You can write in custom attributes for entity properties generated via EF. I've done this several times, and after figuring out how to do it, it now saves a lot of time. I've tried using partial classes before as you mention, but my EF-generated class ends up overwriting the other with the custom attributes. Perhaps I was doing something wrong, but in any case, I now prefer the T4 template solution because it seems cleaner to me - minimizing on number of classes within a project.

    Also, when you update your EF model from DB and the class is regenerated, your custom attributes are still in place. ftw!

    ADDED: By the way, we typically define our model objects within the Data layer to have them mapped/populated by EF's entities. Or (even simpler) use the EF-generated entities all the way through to the UI layer without custom defined POCOs.

提交回复
热议问题