'Circular dependency error during creating migration
I have two entities that are related by a foreign key with a one-to-many relationship. when I try to perform a migration, I get an error related to the circular dependency. i tried to create an OnDelete.Cascade rule in DbContext but the error is still the same.
modelBuilder.Entity<Image>()
.HasOne<Part>(i =>i.Part)
.WithMany(a =>a.Images)
.HasForeignKey(i=>i.PartId)
.OnDelete(DeleteBehavior.Cascade);
System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved: 'Project.Models.Part [Deleted] Image { 'ImageId' } <- Project.Models.Image [Deleted] Images { 'PartId' } <- Project.Models.Part [Deleted]
public class Part
{
[Column("PartId")]
public Guid Id { get; set; }
public ICollection<Image> Images { get; set; }
}
public class Image
{
[Column("ImageId")]
public Guid Id { get; set; }
public bool isPoster { get; set; }
public string Name { get; set; }
public string Path { get; set; }
[ForeignKey(nameof(Part))]
public Guid PartId { get; set; }
public Part Part { get; set; }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
