I have the following Jobcard
class with has two subclases MechanicalJobcard
and PanelbeatingJobcard
public abstract class Jobcard : Entity, IAggregateRoot
{
public int VehicleId { get; private set; }
public int JobcardTypeId { get; protected set; }
public string Opened_By { get; private set; } //TODO Identity Sever
public DateTime Open_Date { get; private set; }
public Vehicle Vehicle { get; private set; }
public JobcardType JobcardType { get; private set; }
public Jobcard()
{
Opened_By = "Job card opener";
Open_Date = DateTime.Now;
}
}
public class PanelBeatingJobcard : Jobcard
{
public PanelBeatingJobcard()
{
JobcardTypeId = 1;
}
}
public class MechanicalJobcard: Jobcard
{
public MechanicalJobcard()
{
JobcardTypeId = 2;
}
}
And i have a vehicle the has both MechanicalJobcard
and PanelbeatingJobcard
public class Vehicle: Entity, IAggregateRoot
{
public int Id{ get; set; }
//Other properties here...
public PanelBeatingJobcard PanelBeatingJobcard { get; set; }
public MechanicalJobcard MechanicalJobcard { get; set; }
}
I can seem to use the fluent API to map these to the database.
I have tried
class PanelBeatingJobCardEntityTypeConfiguration: IEntityTypeConfiguration<PanelBeatingJobcard>
{
public void Configure(EntityTypeBuilder<PanelBeatingJobcard> panelBeatinglJobcardConfig)
{
panelBeatinglJobcardConfig.HasOne(j => j.Vehicle).WithOne(v => v.PanelBeatingJobcard).HasForeignKey<Jobcard>(j => j.VehicleId);
}
}
class MechanicalJobcardEntityTypeConfiguration: IEntityTypeConfiguration<MechanicalJobcard>
{
public void Configure(EntityTypeBuilder<MechanicalJobcard> mechanicalJobcardConfig)
{
mechanicalJobcardConfig.HasOne(j => j.Vehicle).WithOne(v => v.MechanicalJobcard).HasForeignKey<Jobcard>(j => j.VehicleId);
}
}
But i get the following error on Add-Migration
:
You are configuring a relationship between 'PanelBeatingJobcard' and 'Vehicle' but have specified a foreign key on 'Jobcard'. The foreign key must be defined on a type that is part of the relationship
if i remove the both configurations i get the following error on Add-Migration
:
Both relationships between 'Jobcard.Vehicle' and 'Vehicle' and between 'MechanicalJobcard' and 'Vehicle.MechanicalJobcard' could use {'VehicleId'} as the foreign key. To resolve this configure the foreign key properties explicitly on at least one of the relationships.
EDIT:
if change the Entity Type Configuration to the following, as suggested in the comments:
class PanelBeatingJobCardEntityTypeConfiguration: IEntityTypeConfiguration<PanelBeatingJobcard>
{
public void Configure(EntityTypeBuilder<PanelBeatingJobcard> panelBeatinglJobcardConfig)
{
panelBeatinglJobcardConfig.HasOne(j => j.Vehicle).WithOne(v => v.PanelBeatingJobcard).HasForeignKey<PanelBeatingJobcard>(j => j.VehicleId);
}
}
class MechanicalJobcardEntityTypeConfiguration: IEntityTypeConfiguration<MechanicalJobcard>
{
public void Configure(EntityTypeBuilder<MechanicalJobcard> mechanicalJobcardConfig)
{
mechanicalJobcardConfig.HasOne(j => j.Vehicle).WithOne(v => v.MechanicalJobcard).HasForeignKey<MechanicalJobcard>(j => j.VehicleId);
}
}
I get duplicate VehicleId
keys (VehicleId
and VehicleId1
) in the Migrations file:
table.ForeignKey(
name: "FK_jobcard_vehicles_VehicleId",
column: x => x.VehicleId,
principalSchema: "dbo",
principalTable: "vehicles",
principalColumn: "VehicleId",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_jobcard_vehicles_VehicleId1",
column: x => x.VehicleId,
principalSchema: "dbo",
principalTable: "vehicles",
principalColumn: "VehicleId",
onDelete: ReferentialAction.Cascade);
question from:
https://stackoverflow.com/questions/65647421/use-table-per-hierarchy-inheritance-for-12-relationship