The relationship cannot be changed .. foreign key properties are not null

I am trying to update an object of type MamConfiguration_V1

which exsit is already in DB

It has several reference elements (among them MamConfigurationToBrowser_V1 )

  private void UpdateEfItem(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi) { itemFromDb.UpdatedDate = DateTime.Now; itemFromDb.Description = itemFromUi.Description; itemFromDb.StatusId = itemFromUi.StatusId; itemFromDb.Name = itemFromUi.Name; itemFromDb.NumericTraffic = itemFromUi.NumericTraffic; itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic; itemFromDb.Type = itemFromUi.NumericTraffic; itemFromDb.MamConfigurationToBrowser_V1.Clear(); for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++) { var elementToAdd = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i); elementToAdd.Browser = mMaMDBEntities.Browsers.Single(browserItem => browserItem.BrowserID == elementToAdd.BrowserID); elementToAdd.MamConfiguration_V1 = itemFromDb; itemFromDb.MamConfigurationToBrowser_V1.Add(elementToAdd); } 

}

I get the following DB runtime error:

The operation failed: the relation cannot be changed because one or more properties of the foreign key are not NULL. When a change in relationship occurs, the corresponding property of the foreign key is set to zero. If the foreign key does not support null values, a new relationship must be defined, another nonzero value must be assigned to the foreign key property, or an object not associated with it must be deleted.

But this is strange. All links are not null:

enter image description here

enter image description here

Upadte 2:

I tried this code:

  try { item.ThrowIfNull("item"); var itemFromDB = GetById(item.ConfigurationId); if (itemFromDB != null) { UpdateEfItem(itemFromDB, item); //mMaMDBEntities.MamConfiguration_V1.Detach(itemFromDB); //mMaMDBEntities.MamConfiguration_V1.Attach(item); //mMaMDBEntities.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Modified); //mMaMDBEntities.ObjectStateManager.ChangeObjectState(itemFromDB, System.Data.EntityState.Modified); mMaMDBEntities.SaveChanges(); return item; } 

}

  private void UpdateEfItem(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi) { itemFromDb.UpdatedDate = DateTime.Now; itemFromDb.Description = itemFromUi.Description; itemFromDb.StatusId = itemFromUi.StatusId; itemFromDb.Name = itemFromUi.Name; itemFromDb.NumericTraffic = itemFromUi.NumericTraffic; itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic; itemFromDb.Type = itemFromUi.NumericTraffic; foreach (var item in itemFromDb.MamConfigurationToBrowser_V1.ToList()) { if (itemFromUi.MamConfigurationToBrowser_V1.All(b => b.BrowserVersionId != item.BrowserVersionId)) { mMaMDBEntities.MamConfigurationToBrowser_V1.DeleteObject(item); } } for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++) { var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i); var item = itemFromDb.MamConfigurationToBrowser_V1.SingleOrDefault(b => b.BrowserVersionId == element.BrowserVersionId); if (item != null) { // copy properties from element to item } else { element.Browser = mMaMDBEntities.Browsers.Single(browserItem => browserItem.BrowserID == element.BrowserID); element.MamConfiguration_V1 = itemFromDb; //have also tried: element.MamConfiguration_V1 = null; //element.MamConfiguration_V1Reference = null; itemFromDb.MamConfigurationToBrowser_V1.Add(element); } } } 

and execute this error:

{"Violation of the UNIQUE KEY constraint" UQ_MamConfigurations_V1 ". Insert a duplicate key into the 'dbo.MamConfiguration_V1' object. Duplicate key value (elad_14Apr_1315). \ R \ nThe application was terminated." }

+1
source share
1 answer

This line ...

 itemFromDb.MamConfigurationToBrowser_V1.Clear(); 

... will not only clear the collection, but will also set the link from the individual items in the collection to the parent itemFromDb to null . With a change of these elements and EF will try to save them in the database. It fails because the (possibly) link is needed and cannot be null .

You should use a different approach, which updates the elements of the collection one at a time. You must consider that items can be deleted in the user interface so that they can be changed and that new items can be added. It will look something like this:

 private void UpdateEfItem(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi) { itemFromDb.UpdatedDate = DateTime.Now; itemFromDb.Description = itemFromUi.Description; itemFromDb.StatusId = itemFromUi.StatusId; itemFromDb.Name = itemFromUi.Name; itemFromDb.NumericTraffic = itemFromUi.NumericTraffic; itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic; itemFromDb.Type = itemFromUi.NumericTraffic; foreach (var item in itemFromDb.MamConfigurationToBrowser_V1.ToList()) { if (!itemFromUi.MamConfigurationToBrowser_V1.Any(b => b.BrowserID == item.BrowserID) { mMaMDBEntities.Browsers.DeleteObject(item); } } for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++) { var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i); var item = itemFromDb.MamConfigurationToBrowser_V1 .SingleOrDefault(b => b.BrowserID == element.BrowserID); if (item != null) { // copy properties from element to item } else { element.Browser = mMaMDBEntities.Browsers.Single(browserItem => browserItem.BrowserID == element.BrowserID); itemFromDb.MamConfigurationToBrowser_V1.Add(element); } } } 
+3
source

Source: https://habr.com/ru/post/894114/


All Articles