2

To map a column in EF6 Code First, we use this code for example :

Property(o => o.Email).HasColumnType("varchar").HasMaxLength(255).IsRequired();

To prevent to write several times ".HasColumnType("varchar").HasMaxLength(255)" for all Email columns (for example), I would want to factorise that and define a EMail configuration that I could use each time I need. I would want do that :

Property(o => o.Email).IsEmailColumn(EMail).IsRequired();

How can I do that ?

Thank you.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
Phil
  • 1,035
  • 2
  • 10
  • 17

1 Answers1

3

You can put the common code in a custom "fluent" extension method like this:

using System.Data.Entity.ModelConfiguration.Configuration;

public static partial class ConfigurationExtensions
{
    public static StringPropertyConfiguration IsEmailColumn(this StringPropertyConfiguration property)
    {
        return property.HasColumnType("varchar").HasMaxLength(255);
    }
}

which allows you to use

Property(o => o.Email).IsEmailColumn().IsRequired();
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343