Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gshutler/339268 to your computer and use it in GitHub Desktop.
Save gshutler/339268 to your computer and use it in GitHub Desktop.
public static class ConventionHelper
{
public static string PluralUnderscore(string name)
{
return Underscore(Inflector.Net.Inflector.Pluralize(name));
}
public static string Underscore(string name)
{
return Regex.Replace(name, "([a-z])([A-Z])", "$1_$2").ToLowerInvariant().Trim();
}
}
public class CustomForeignKeyConvention : ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
if (property == null)
return ConventionHelper.Underscore(type.Name) + "_id"; // many-to-many, one-to-many, join
return ConventionHelper.Underscore(property.Name) + "_id"; // many-to-one
}
}
public class IdConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column("id");
}
}
public class PropertyConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
instance.Column(ConventionHelper.Underscore(instance.Property.Name));
}
}
public class ReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.Column(ConventionHelper.Underscore(instance.Property.Name) + "_id");
}
}
public class TableConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(ConventionHelper.PluralUnderscore(instance.EntityType.Name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment