Extending Entity Framework Model to include new property

后端 未结 3 1046
攒了一身酷
攒了一身酷 2020-12-16 04:04

I\'m new to EF so please excuse me if this is a noob question.

Basically, we have a EF model set up using Model First for our \'platform\' project and is shared acro

相关标签:
3条回答
  • 2020-12-16 04:24

    I agree with adding additional properties to partial class of your entities (as you and Kaido said).

    This way you can freely add the properties you want, without modifying generated classes and if you generate your model again (or update it from DB), your partial class is not modified.

    In my opinion, adding properties to partial classes of generated entities is the way to go.

    0 讨论(0)
  • 2020-12-16 04:25

    You cannot use inheritance because once entity is loaded from the data source EF will not know about inheritance and because of that it will instantiate base type without your properties instead of derived type with your properties. Any inheritance must be mapped in EDMX if EF have to work with it.

    Using partial class will solve your problem but:

    • All parts of partial class must be defined in the same assembly
    • Properties from your partial part are not persisted to the database
    • Properties from your partial part cannot be used in linq-to-entities queries
    0 讨论(0)
  • 2020-12-16 04:26

    EF generates partial classes. So to extend MyEntity, create a MyEntity.cs file with

    partial class MyEntity
    {
        public string MyExtraProperty {get;set;}
    }
    

    edit: in the same namespace as your generated entities

    0 讨论(0)
提交回复
热议问题