Entity Framework 6: Right usage of "Custom Code First Conventions" Feature?

entity-framework entity-framework-6

Question

My requirement is to configure the string length mapping globally, but I can also configure a property specifically using the MaxLengthAttribute. Below is my code:

public class StringLengthConvention
: IConfigurationConvention<PropertyInfo, StringPropertyConfiguration>
{
    public void Apply(
        PropertyInfo propertyInfo,
        Func<StringPropertyConfiguration> configuration)
    {
        StringAttribute[] stringAttributes = (StringAttribute[])propertyInfo.GetCustomAttributes(typeof(StringAttribute),true);
        if (stringAttributes.Length > 0)
        {
            configuration().MaxLength = stringAttributes [0].MaxLength;
        }
    }
}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {           
        modelBuilder.Conventions.Add<StringLengthConvention>();
    }

public class ContentInfo
{
   // ...
    [MaxLength(200)]
    [String]        
    public string TitleIntact { get; set; }
   // ...
}

The "MaxLength" can no longer function, which is my issue. Do I need to confirm that the property has a MaxLengthAttribute before applying the StringLengthConvention's global configuration? Apply()?

1
1
12/18/2012 10:43:47 AM

Accepted Answer

ZZZ_tmp
2
12/19/2012 7:23:48 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow