Created
October 20, 2015 21:18
-
-
Save crmckenzie/dab59eb1c774332bf955 to your computer and use it in GitHub Desktop.
EF 6 Conventions example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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