I’m using EF 4.3 and am hitting an Oracle database. Because of how EF generates SQL for the Oracle database, and because EF 4.3 doesn’t allow adding custom conventions, I have to manually specify column names for each property in ALL CAPS.
[Column("MYPROPERTY")]
public string MyProperty { get; set; }
Well, what can I do about properties that use complex types? I have the following complex type:
public class Minute {
public int Value { get; set; }
}
and the following DbContext:
public class MyDbContext : DbContext {
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<Minutes>().Property(x => x.Value);
}
}
And my entity:
[Table("MYENTITY")]
public class MyEntity {
[Column("MYPROPERTY")]
public string MyProperty { get; set; }
[Column("MYMINUTES")] //this does nothing
public Minutes MyMinutes { get; set; }
}
In the generated SQL, the MyMinutes property is mapped to a non-existent "Extent1"."MyMinutes_Value"… I wanted "Extent1"."MYMINUTES".
What can I do here besides bang my head on my desk?
The answer is fairly simple, but not very well documented. As far as I know, it can’t be done with attributes. Add a line to the datacontext’s OnModelCreating method:
So, the first line in the method sets up the complex type in general. The second line tells EF how to map the property on the MyEntity class, specifically. Viola!