我正在尝试更新类型的实体MamConfiguration_V1
已经存在于数据库中
它的参考成员很少(其中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);
}
}
我收到以下数据库运行时错误:
操作失败:无法更改关系,因为一个或多个外键属性不可为空。当对关系进行更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。
但这很奇怪。所有引用都不为空:
更新2:
我试过这段代码:
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);
}
}
}
并出现此错误:
{"违反 UNIQUE KEY 约束 'UQ_MamConfigurations_V1'。无法在对象 'dbo.MamConfiguration_V1' 中插入重复键。重复键值为 (elad_14Apr_1315)。\r\n语句已终止。"}