0

Using EF 6.1.2. CodeFirst. I have these models:

public class Automobile
{
    public int Id { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public string Vin { get; set; }
    public int Year { get; set; }
    public Engine EngineType { get; set; }
    public Transmission TransmissionType { get; set; }
    public DriveTrain DriveTrainType { get; set; }
    public ICollection<Accessory> Accessories { get; set; }
}

public class Transmission
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int Speeds { get; set; }
    public bool IsAutomatic { get; set; }
}

public class Accessory
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int AutomobileId { get; set; }
}

public class DriveTrain
{
    public bool IsAwd { get; set; }
    public bool IsHeavyDuty { get; set; }
    public string Name { get; set; }
}

public class Engine
{
    public int Id { get; set; }
    public string  Size { get; set; }
    public string Brand { get; set; }
    public int cylinders { get; set; }
    public int AutomobileId { get; set; }
    public ICollection<Automobile> AutoMobiles { get; set; } 
}

My storage goal is to use entity splitting and end up with only 2 tables: Automobile, and Accessories. Because the Transmission entity, and DriveTrain entity both do not have primary keys, EF6 automatically merges those tables into my main table Automobile for storage. This is great default behavior and I like it. My problems comes when addressing the Engine entity. Notice that it has a primary key which is conceptually different than the Automobile Primary key. Having already used a few fluent API configurations, I have not been able to successfully merge the Engine entity into the Automobile entity when it comes to storage. I found this MSDN article: http://msdn.microsoft.com/en-us/data/JJ591617.aspx#2.8 but this example requires the same shared primary key. These two entities have different primary keys.

What is the best way to accomplish my goal while maintaining the exact schema as identified above?

Lorne Redmond
  • 263
  • 1
  • 3
  • 13

0 Answers0