简体   繁体   中英

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:

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:

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>> .

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 ). 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.

Is this configuration at all possible?

It seems to me that your PromotionVehicle is a table that links other tables (a relationship table)

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

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) ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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