i've an unexpected issue trying to remove child entity from parent single database record. After doing some tests we have replicated the problem. Our C# code use Northwind database.
NWModel context = new NWModel();
Orders order = context.Orders.Where(w => w.OrderID == 10248).FirstOrDefault();
context.Entry(order).Collection(typeof(Order_Details).Name).Load();
order.Order_Details.RemoveAt(1);
System.Data.Entity.Infrastructure.DbChangeTracker changeset = context.ChangeTracker;
var changes = changeset.Entries().Where(x => x.State == System.Data.Entity.EntityState.Modified).ToList<Object>();
var detetions = changeset.Entries().Where(x => x.State == System.Data.Entity.EntityState.Deleted).ToList<Object>();
All works fine with original Order_Details table setup.
var deletions correctly has got deleted record.
In order to reproduce the issue, we have added on Order_Details table a new PK identity int field (OrderDetailId int identity); after doing that: var deletions contain no record while var changes contain Order_Detail record.
EF set Orders property of Order_Detail to null and mark the record as Updated.
I've found a lot of articles regarding this issue, all of them suggests to mark Order_Detail Orders property to [Required].
I've tried to set [Required] attribute on FK entity as suggested in this post, (this article describe EFCore behaviour that is the same as EF6 behaviour) but is does not solve my issue.
Is this behaviour expected?
we would appreciate any comments or suggestions.
Thanks