OK, I don't have exact solution for you, but have a similar situation. EF Generated fields ends with ID if when referencing another table. Example. Table Person has a column JobID instead of Job. We don't like that. This was the issue: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/f8ddb2be-78c9-4296-b293-37b7bc8e8fd7
So what my team mate did is to "override" the default convention. Please forgive if this is not exactly your case. I think it should give you some hints. This is with EF V 4.1
public class ASRCDb : DbContext, IUnitOfWork
{
public void Complete()
{
SaveChanges();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
AddAllEntities(modelBuilder);
}
static void AddAllEntities(DbModelBuilder modelBuilder)
{
var entity = typeof(DbModelBuilder).GetMethod("Entity");
foreach (var entityType in GetEntityTypes())
{
var entityTypeConfiguration = entity.MakeGenericMethod(entityType).Invoke(modelBuilder, null);
foreach (var propertyInfo in GetReferenceProperties(entityType))
ConfigureRelationship(propertyInfo, entityTypeConfiguration);
}
}
static IEnumerable<PropertyInfo> GetReferenceProperties(Type entityType)
{
return entityType.GetProperties().Where(p => typeof(IEntity).IsAssignableFrom(p.PropertyType));
}
static IEnumerable<Type> GetEntityTypes()
{
return typeof(Entity).Assembly.GetTypes().Where(type => type.IsClass && !type.IsAbstract);
}
static void ConfigureRelationship(PropertyInfo propertyInfo, dynamic entityTypeConfiguration)
{
var required = propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), true).Any();
var navigation = required
? entityTypeConfiguration.HasRequired(GetPropertyExpression(propertyInfo))
: entityTypeConfiguration.HasOptional(GetPropertyExpression(propertyInfo));
UsePropertyNameAsColumnName(propertyInfo, navigation);
}
static void UsePropertyNameAsColumnName(PropertyInfo propertyInfo, dynamic navigation)
{
Action<ForeignKeyAssociationMappingConfiguration> mapKey = x => x.MapKey(propertyInfo.Name);
navigation.WithMany().Map(mapKey);
}
static dynamic GetPropertyExpression(PropertyInfo propertyInfo)
{
var parameter = Expression.Parameter(propertyInfo.ReflectedType);
return Expression.Lambda(
typeof(Func<,>).MakeGenericType(propertyInfo.ReflectedType, propertyInfo.PropertyType),
Expression.Property(parameter, propertyInfo),
parameter);
}
}
All the Credit is to my buddy Diego Mijelshon.