ntziolis
ntziolis

Reputation: 10221

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)");

Upvotes: 3

Views: 1053

Answers (2)

Kyle
Kyle

Reputation: 1172

[System.ComponentModel.DataAnnotations.MaxLength]

Upvotes: 5

Kim Tranjan
Kim Tranjan

Reputation: 4541

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>();
}

Upvotes: 7

Related Questions