Entity-framework-4 – ef4 codefirst ctp5 nvarchar (max) via attribute

Is there a way to create a custom attribute so that EF codefirst uses nvarchar (max) as the data type when assigning attributes to poco classes? I know this can be achieved through a smooth API, but we want to put all the definitions in one place, which is the metadata class

Fluent API:

modelBuilder.Entity<Event>().Property(p => p.TicketText).HasColumnType("nvarchar(max)");

Solution

public class NVarcharMaxAttribute : Attribute { }
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>();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>