Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

Base Entity class for the sake of ID in EF Code First

Does it makes sense to introduce an abstract IdableEntity base class, that has a public Guid Id { get; set;} property, and then derive all my entities from it, so I don't have to add key field to each one of them? Will I face any pitfalls if I do in theory?

I used to never do this since my DbContext model was rather simple and consisted of a few classes. Now I am designing a large entity number model that may benefit from it.

Upvotes: 1

Views: 157

Answers (1)

slugster
slugster

Reputation: 49974

Speaking from experience of this exact thing - yes it makes sense. Note that this EF base class doesn't have to be the heirarchical root though - it could inherit from a base class that implements things like INotifyPropertyChanged, IChangeTracking, etc. So your inheritance chain would look like this:

BaseDataClass <- BaseEFDataObject <- [actual EF entities]

Upvotes: 1

Related Questions