3

EF CodeFirstがpocoクラスのプロパティに割り当てられたときにデータ型としてnvarchar(max)を使用するようにするカスタム属性を作成する方法はありますか?これは流暢なAPIを介して可能であることを私は知っていますが、すべての定義を1つの場所に入れたいので、それがメタデータクラスです。

流暢なAPI:

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

2 に答える 2

7
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>();
}
于 2011-02-22T20:25:01.157 に答える
5
[System.ComponentModel.DataAnnotations.MaxLength]
于 2011-03-29T21:32:19.250 に答える