EF4 CodeFirst CTP5 nvarchar (max) via attribute

Is there a way to create a custom attribute that allows you to use EF CodeFirst nvarchar (max) as a data type when assigning a property of the poco class? I know this is possible thanks to a smooth api, but we would like to have all the definitions in one place, and this is a metadata class.

Fluent API:

modelBuilder.Entity<Event>().Property(p => p.TicketText).HasColumnType("nvarchar(max)");
+3
source share
2 answers
public class NVarcharMaxAttribute : Attribute { }

public class NVarcharMaxAttributeConvention : AttributeConfigurationConvention<PropertyInfo, StringPropertyConfiguration, NVarcharMaxAttribute> {
    public override void Apply(PropertyInfo memberInfo, StringPropertyConfiguration configuration, NVarcharMaxAttribute attribute) {
        configuration.ColumnType = "nvarchar(max)";
    }
}

protected override void OnModelCreating(ModelBuilder modelBuilder) {
    modelBuilder.Conventions.Add<NVarcharMaxAttributeConvention>();
}
+7
source
[System.ComponentModel.DataAnnotations.MaxLength]
+5
source

Source: https://habr.com/ru/post/1793977/


All Articles