Entity Framework DELETE statement conflicted with the REFERENCE constraint
You can resolve this issue on SQL side
Method 1 :
First, you need to find on which table this
FK
constraint has been defined, through usingReplication monitor
.Right click on that
FK
, clickModify
, you should get popup box like one shown below.
- From the popup box, Select
Cascade
fordel
.
Method 2 :
set ON DELETE CASCADE
in sql at the end of constraint.
Can't really tell much from what you have said, but you may benefit from looking into using the DbModelBuilder to solve cascade issues:
modelBuilder.Entity<Parent>()
.HasMany<Child>(c => c.Children)
.WithOptional(x => x.Parent)
.WillCascadeOnDelete(true);
Again, would need more information about your model structure to determine if this is the right approach.
Either that or in your delete method, remove any children first, and then remove the parent.
modelBuilder.Entity<Parent>()
.HasMany<Child>(c => c.Children)
.WithOptional(x => x.Parent)
.WillCascadeOnDelete(true);
or use Include
var adv = db.Adv.Include(b => b.Features)
.Include(b => b.AdvDetails)
.Include(b => b.AdvGallery)
.FirstOrDefault(b => b.Id == id);
db.Adv.Remove(adv);
for .HasMany(...).WithMany(...) Include is ok