Some time ago I asked a question about creating a custom convention for EF6, and the answer provided worked out quite well.
Now, I'm running into a case where I'm using this and want to override the value for a particular column, but can't find out how to do so.
Here's the convention I've added:
class ColumnPrefixConvention : IStoreModelConvention<EdmProperty>
{
public void Apply(EdmProperty property, DbModel model) {
string name = property.Name;
// check if this is an ID field
if (name == "ID") {
return;
}
// Check if this is a foreignID field
if (name.Right(2) == "ID") {
return;
}
property.Name = property.DeclaringType.Name + property.Name;
}
}
And here's the model builder:
protected override void OnModelCreating(DbModelBuilder mb) {
mb.Conventions.Add(new ColumnPrefixConvention());
mb.Entity<ClientRate>().Property(x => x.Rate).HasColumnName("HourlyRate");
mb.Entity<ClientRate>().Property(x => x.EffectiveDate).HasColumnName("EffectiveDate");
}
As you can see, I'm trying to override the HourlyRate
and EffectiveDate
columns, which do not follow my convention.
I would have thought that specifying the HasColumnName
on the entity property level would have taken precedence over the convention, but that's not the case: I'm getting an Invalid Column Name error that shows it's still trying to prefix the table name per the ColumnPrefixConvention I've added.
Does anyone know a way around this? Thanks