2021年5月4日星期二

C# EF Core - How to define a field ONCE, then use that definition in multiple entities

I am using class inheritance to add a set of basic fields that all my entities must include. When I do this, EF Core honors any data annotations that exist in the base class, such as [TimeStamp], on the class that is inheriting from it:

public class SomeClass : BaseEntity  {      public string SomeEntityField { get; set; }  }    public class BaseEntity  {      public int Id { get; set; }      public DateTimeOffset UpdatedDate { get; set; }      public DateTimeOffset CreatedDate { get; set; }        [Timestamp]      public byte[] RowVersion { get; set; }  }  

The above is great when I want to add all inherited fields.

But, what if I wanted to to be able to create a field definition ONCE, and then use that definition in just a few other entities whilst also inheriting from the base class (above) at the same time?

Lets suppose I have this entity, ItemType:

public class ItemType : BaseEntity  {      [StringLength(28)]      public string ItemTypeId { get; set; }      ...  

I'd like to be able to define the field ItemTypeId in one place, and then use it on multiple entities including the above table WITHOUT having to manually add/maintain the annotation(s) in all those entities. For example, if I need to make the field wider like [StringLength(50)], I adjust it in one place and then all associated fields are updated on the next migration sinve they al 'read' from the same definition.

  • I've tried using class interfaces, but data annotations on interfaces are not honored by EF.
  • Multiple inheritance cannot be used bacause I may need multiple "one-off" fields (and interfaces don't work as per first list point)

This means that nothing as per here will work (ok, with the exception of the single base class itself).

I think ideally I'd like to be able to define the entity fields in CommonFieldType for example as below, and then have target entities inherit from them, but unfortunately that can't work for the reasons above.

namespace RDCApp.Shared.CommonFieldType  {        public abstract class ItemTypeIdCFT      {          [StringLength(28)]          public string ItemTypeId { get; set; }      }        public abstract class AnotherCFT      {          [StringLength(100)]          public string AnotherField { get; set; }      }        ...  

I can't be the only person who needs/would like this so is there another way? I am aware that Fluent API could be used, but my project is so invested data annotations that using that (as well as) feels wrong, and suddenly means I have 2 places look in and define field attributes against. Defining against the POCO's directly just feels right to me!

https://stackoverflow.com/questions/67392508/c-sharp-ef-core-how-to-define-a-field-once-then-use-that-definition-in-multip May 05, 2021 at 05:34AM

没有评论:

发表评论