使用EF 6.1.2。 CodeFirst。我有这些模型:
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; }
}
我的存储目标是使用实体拆分,最终只有2个表: Automobile 和 Accessories 。 由于传输实体和 DriveTrain 实体都没有主键,因此EF6会自动将这些表合并到我的主表 Automobile 中进行存储。这是很好的默认行为,我喜欢它。解决引擎实体时出现问题。请注意,它的主键在概念上与 Automobile 主键不同。已经使用了一些流畅的API配置,在存储方面,我无法将 Engine 实体成功合并到 Automobile 实体中。我发现这篇MSDN文章: http://msdn.microsoft.com/en-us/data/JJ591617.aspx#2.8 但是此示例需要相同的共享主键。这两个实体具有不同的主键。
在保持上述确切模式的同时实现目标的最佳方法是什么?