我基于这个链接实现了一个连接表。
public class Car
{
public int ID { get; set; }
public string Brand { get; set; }
public string Type { get; set; }
public CarDriver CarDriver { get; set; }
}
public class Driver
{
public Driver()
{ }
public int ID { get; set; }
public string Name { get; set; }
public CarDriver CarDriver { get; set; }
}
public class CarDriver
{
public int CarID { get; set; }
public Car Car { get; set; }
public int DriverID { get; set; }
public virtual Driver Driver { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var carDriverEtb = modelBuilder.Entity<CarDriver>();
carDriverEtb.HasKey(cd => new { cd.CarID, cd.DriverID });
carDriverEtb.HasIndex(cd => cd.CarID).IsUnique();
carDriverEtb.HasIndex(cd => cd.DriverID).IsUnique();
}
我想删除自动(级联)都CarDriver和司机时,汽车将被删除。但只有CarDriver记录将被删除。
如有任何建议或协助,将不胜感激。
你可以做为
builder.Entity<Car>()
.HasOne(e => e.CarDriver)
.WithOne(qs => qs.Car)
.HasForeignKey(qsg => qsg.CarID)
.OnDelete(DeleteBehavior.Cascade);
并且您的映射模型CarDriver
没有唯一的id。