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:


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) {
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." }
source share