EF4 CodeFirst CTP5 nvarchar(max) via attribute
문제
Is there a way to create a custom attribute that will make EF CodeFirst use nvarchar(max) as datatype when assigned to a property of a poco class? I know this is possible via fluent api, but we would like to have all definitions within one place and thats the metadata class.
Fluent API:
modelBuilder.Entity<Event>().Property(p => p.TicketText).HasColumnType("nvarchar(max)");
해결책
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>();
}
다른 팁
[System.ComponentModel.DataAnnotations.MaxLength]
제휴하지 않습니다 StackOverflow