For a couple of days ago I got the question of “my mappings doesn’t work”. I looked at it and found that the database tables were generated upfront and not by the ObjectContext. The columnnames had other names. Than how-do you solve this missmatch if you really do want to have separate namings? Using MapSingleType.
Personally, I let the context create the database and the tables, since I want the model to drive the database design.
The entity
[Serializable]
public class Car
{
public string LicencePlate { get; set; }
public int CountryCode { get; set; }
}
The mapping
[Serializable]
public class CarMapping : EntityConfiguration<Car>
{
public CarMapping()
{
HasKey(c => new { c.CountryCode, c.LicencePlate });
MapSingleType(c =>
new
{
TheCountryCode = c.CountryCode,
TheLicencePlate = c.LicencePlate
}).ToTable("dbo.TheCars");
}
}
The result
Table: TheCars (you don’t have to use “ToTable”).
Columns:
- TheCountryCode
- TheLicencePlate
//Daniel
This is really a series on CodeOnly not EF4.
Hi!
Correct, but I’m using it along with EF4
//Daniel
Pingback: Entity framework 4 – CTP3 – Code first vs Linq to Sql « Daniel Wertheim