You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
EF Core should honor [MaxLength] attributes from interfaces an Entity Implements
It's less useful and frustrating for a user of EFCore to spend a lot of time developing Interfaces for defining categories of Entities which include a [MaxLength] attribute on a property of the interface, only to find that the migration generated has a nvarchar max column type, and thus have to go back and manually add [MaxLength] to each property on each entity that implements that interface.
Honestly, a user of EF Core would intuitively expect this work. And it's both surprising and frustrating that it doesn't.
EF will generate a migration for that has ReferenceId as nvarchar(max)
I'd like EF to use reflection to check scan for interfaces an entity implements and get a list of properties from those interfaces, if they have a max length attribute on them, apply them to their counterpart property in the entity.
So that it will generate an appropriate sized column based on the interface and improved the usefulness and value proposition of using Entity Framework Core in general.
Here is a Naive implementation:
protectedvoidSetUpMaxLengthViaInterfaces(ModelBuildermodelBuilder){foreach(var entityType in modelBuilder.Model.GetEntityTypes()){varclrType= entityType.ClrType;varinterfaces= clrType.GetInterfaces();foreach(var interfaceType in interfaces){varinterfaceProperties= interfaceType.GetProperties();foreach(var interfaceProperty in interfaceProperties){varmaxLengthAttribute= interfaceProperty.GetCustomAttribute<MaxLengthAttribute>();if(maxLengthAttribute!=null){varentityProperty= clrType.GetProperty(interfaceProperty.Name);if(entityProperty!=null){
modelBuilder.Entity(clrType).Property(entityProperty.Name).HasMaxLength(maxLengthAttribute.Length);}}}}}}
The text was updated successfully, but these errors were encountered:
EF Core should honor [MaxLength] attributes from interfaces an Entity Implements
It's less useful and frustrating for a user of EFCore to spend a lot of time developing Interfaces for defining categories of Entities which include a [MaxLength] attribute on a property of the interface, only to find that the migration generated has a nvarchar max column type, and thus have to go back and manually add [MaxLength] to each property on each entity that implements that interface.
Honestly, a user of EF Core would intuitively expect this work. And it's both surprising and frustrating that it doesn't.
here is an example:
EF will generate a migration for that has ReferenceId as nvarchar(max)
I'd like EF to use reflection to check scan for interfaces an entity implements and get a list of properties from those interfaces, if they have a max length attribute on them, apply them to their counterpart property in the entity.
So that it will generate an appropriate sized column based on the interface and improved the usefulness and value proposition of using Entity Framework Core in general.
Here is a Naive implementation:
The text was updated successfully, but these errors were encountered: