'Error while introducing foreign key constraint
I'm getting an error while trying to EF database update.
I have 2 entities, Flight and City:
public class Flight : Entity<Guid>
{
public Airline Airline { get; set; } = default!;
public string CheckInTime { get; set; } = default!;
public string CheckOutTime { get; set; } = default!;
public Guid DepartureCityId { get; set; }
public Guid DestinyCityId { get; set; }
public City DepartureCity { get; set; } = default!;
public City DestinyCity { get; set; } = default!;
public DateTime CheckOutDate { get; set; }
public FlightStatusEnum FlightStatus { get; set; }
}
public class City : Entity<Guid>
{
public string Name { get; set; } = default!;
}
Model builder:
modelBuilder.Entity<Flight>(
entity =>
{
entity.HasKey(f => f.Id);
... // other properties config
entity.HasOne(f => f.DepartureCity).WithMany().HasForeignKey(f => f.DepartureCityId)
.OnDelete(DeleteBehavior.NoAction);
entity.HasOne(f => f.DestinyCity).WithMany().HasForeignKey(f => f.DestinyCityId)
.OnDelete(DeleteBehavior.NoAction);
});
When I try to run ef database update, I'm getting this error
Introducing foreign key constraint 'FK_Flight_City_DestinyCityId' on table 'Flight' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
I've tried creating two list for each flight type in city entity, also changing the DeleteBehavior to Restrict and NoAction, but I still get the same error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
