var main = (from d in db.Discounts where d.Id == discount.Id && d.UserId == userId select d).FirstOrDefault();
main.Address = "new address";
main.TermsStocks.Clear(); // I need clear collection and add new object.
foreach (var termsStock in terms)
{
main.TermsStocks.Add(new TermsStock() { Id = Guid.NewGuid(), MainId = main.Id, Title = termsStock.Title });
}
db.SaveChanges();
and I have error:
The operation failed. Unable to change the link because one or more
properties of the foreign key values do not allow NULL. If you
change the connection property of the respective foreign key set to
NULL. If the foreign key value does not support NULL, must be defined
a new relationship, the foreign key property should be set to another
value other than NULL, or to remove unbound object.
how to remove the entire collection and save a new one?
You will have to remove those items form the database as well, not just from the nav property.
Roughly: