Skip to content

Instantly share code, notes, and snippets.

@crmckenzie
Created October 20, 2015 21:18
Show Gist options
  • Save crmckenzie/dab59eb1c774332bf955 to your computer and use it in GitHub Desktop.
Save crmckenzie/dab59eb1c774332bf955 to your computer and use it in GitHub Desktop.
EF 6 Conventions example
public class StringVarcharFieldConvention : Convention
{
public StringVarcharFieldConvention()
{
this.Properties()
.Where(p => p.PropertyType == typeof(string))
.Configure(c => c.HasColumnType("varchar").HasMaxLength(250))
;
}
}
public class AllCapsAndUnderscoresTableNameConvention : Convention
{
private string GetTableName(Type type)
{
return type.Name.CamelCaseToUnderscore().ToUpper();
}
public AllCapsAndUnderscoresTableNameConvention()
{
this.Types().Configure(t => t.ToTable(GetTableName(t.ClrType)));
}
}
public class LowerCaseAndUnderscoresColumnNameConvention : Convention
{
private string GetColumnName(string name)
{
return name.CamelCaseToUnderscore().ToLower();
}
public LowerCaseAndUnderscoresColumnNameConvention()
{
base.Properties()
.Configure(row =>
{
row.HasColumnName(GetColumnName(row.ClrPropertyInfo.Name));
});
}
}
public static class StringExtensions
{
public static string CamelCaseToUnderscore(this string input)
{
var result = Regex.Replace(input, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment