Skip to content

Instantly share code, notes, and snippets.

@ianfnelson
Last active April 1, 2024 19:33
Show Gist options
  • Save ianfnelson/7ec3a22c992425ebe555 to your computer and use it in GitHub Desktop.
Save ianfnelson/7ec3a22c992425ebe555 to your computer and use it in GitHub Desktop.
Snippets for 2011 blog post "Entity Framework Week Part 2: Conventions and Fluent Mappings"
public class MyAppPrimaryKeyConvention : IConfigurationConvention<PropertyInfo, PrimitivePropertyConfiguration>
{
public void Apply(PropertyInfo memberInfo, Func<PrimitivePropertyConfiguration> configuration)
{
if (memberInfo.Name == "Id")
{
// We need to avoid overriding concrete class mapped using TPH.
if (memberInfo.ReflectedType.IsSubclassOf(typeof(ProductFeature)) ||
memberInfo.ReflectedType.IsSubclassOf(typeof(ProductInsert)))
{
return;
}
configuration().ColumnName = string.Format("{0}Id", memberInfo.ReflectedType.Name);
}
}
}
public class MyAppPrimaryKeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column(string.Format("{0}Id", instance.EntityType.Name));
}
}
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
References(x => x.AudioFormat).Column("AudioFormat");
HasOptional(x => x.AudioFormat)
.WithMany()
.IsIndependent()
.Map(m => m.MapKey(a => a.Id, "AudioFormat"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment