简体   繁体   English

将多个实体映射到单个表

[英]Mapping multiple Entities to a single table

I have the following (example) Entities:我有以下(示例)实体:

public abstract class Vehicle<TVehicleDetails> where TVehicleDetails : VehicleDetails
{
    public int VehicleId { get; set; }
    public TVehicleDetails? Details { get; set; }
}
public class Car : Vehicle<CarDetails> { }
public class Truck : Vehicle<TruckDetails> { }

I'm able to map the Entities Car and Truck with no issues:我能够 map 实体CarTruck ,没有任何问题:

modelBuilder.Entity<Car>().OwnsOne(p => p.Details).WithOwner();
modelBuilder.Entity<Truck>().OwnsOne(p => p.Details).WithOwner();

I also have the following (example) Entity:我还有以下(示例)实体:

public class PromotionVehicle<TVehicle, TVehicleDetails>
    where TVehicle : Vehicle<TVehicleDetails>
    where TVehicleDetails : VehicleDetails
{
    public int PromotionVehicleId { get; set; }
    public TVehicle { get; set; }
    public int VehicleId { get; set; }
    public VehicleType VehicleType { get; set; } // this is just an enum
}

This is how I'm trying to map PromotionVehicle<Car, CarDetails> and PromotionVehicle<Truck, TruckDetail> to the same table but to no avail:这就是我尝试 map PromotionVehicle<Car, CarDetails>PromotionVehicle<Truck, TruckDetail>到同一张表但无济于事的方式:

modelBuilder.Entity<PromotionVehicle<Car, CarDetails>>().ToTable("PromotionVehicles");
modelBuilder.Entity<PromotionVehicle<Truck, TruckDetails>>().ToTable("PromotionVehicles");
modelBuilder.Entity<PromotionVehicle<Vehicle<VehicleDetails>, VehicleDetails>>>().ToTable("PromotionVehicles").HasDiscriminator(p => p.VehicleType).HasValue<PromotionVehicle<Car, CarDetails>>(VehicleType.Car).HasValue<PromotionVehicle<Truck, TruckDetails>>(VehicleType.Truck);

But, when attempting to create Migrations , understandably, EntityFramework throws an Exception with an error message mentioning PromotionVehicle<Car, CarDetails> is not a descendent of PromotionVehicle<Vehicle<VehicleDetails>, VehicleDetails>> .但是,当尝试创建Migrations时,可以理解的是,EntityFramework 会抛出一个Exception ,并显示一条错误消息PromotionVehicle<Car, CarDetails> is not a descendent of PromotionVehicle<Vehicle<VehicleDetails>, VehicleDetails>>

Effectively what I'm trying to do is tell EntityFramework that when it looks at the "PromotionVehicles" table, the column VehicleType should tell it which "vehicle table" to look at and then load the appropriate Vehicle<> Entity (using the IQueryable<>.Include ).实际上,我想做的是告诉 EntityFramework,当它查看“PromotionVehicles”表时, VehicleType列应该告诉它要查看哪个“车辆表”,然后加载适当的Vehicle<>实体(使用IQueryable<>.Include ). I believe the necessary bits of information are there to tell EntityFramework where to look , the VehicleType determines whether to look at "Cars" or "Trucks" and the VehicleId satisfies the ForeignKey requirement.我相信那里有必要的信息位告诉 EntityFramework 去哪里VehicleType决定是看“Cars”还是“Trucks”, VehicleId满足ForeignKey要求。

Is this configuration at all possible?这种配置完全可能吗?

It seems to me that your PromotionVehicle is a table that links other tables (a relationship table)在我看来,您的PromotionVehicle是一个链接其他表(关系表)的表

If that is correct you could just store the Ids of the related tables, use EF to store and retrieve those Ids, and then use those Ids to loading each entity in a custom "LoadPromotionVehicle" method如果这是正确的,您可以只存储相关表的 ID,使用 EF 存储和检索这些 ID,然后使用这些 ID 在自定义“LoadPromotionVehicle”方法中加载每个实体

Here is some seudo code这是一些伪代码

public class PromotionVehicleRelations
{
    public int PromotionVehicleId { get; set; }
    public Int VehicleId { get; set; } //store just IDs not entity
    public int VehicleDetailsID { get; set; } //store just IDs not entity
    public VehicleType VehicleType { get; set; } // enums
}

PromotionVehicle pv ;
PromotionVehicleRelations pvr ;
PromotionVehicleId pvID = 1234 ;

//load the table with the IDs using regular EF mapping
pvr = EF.load(pvID) ;

//populate a new entity with full details that you need using the IDs
pv = LoadPromotionVehicle(pvr) ;

LoadPromotionVehicle(PromotionVehicleRelations pvr) {
  pv.TVehicle = EF.loadVehicle(pvr.VehicleId)
  pv.VehicleDetails = EF.loadVehicleDetails(pvr.VehicleDetailsID) ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM