这样做可以接受吗?首先尝试添加实体。如果添加失败,没关系,因为这意味着实体已经存在?
还是有更优雅/更简单的解决方案?
EntityFrameworkEntities dal = EntityDataModelHelper.GetEntityDataModel();
try
{
dal.AddToXXXXXX(xxxxxxx);
}
catch
{
}
try
{
dal.SaveChanges();
return true;
}
catch
{
return false;
}
好的,我将其缩短为...
EntityFrameworkEntities dal = EntityDataModelHelper.GetEntityDataModel();
if(xxxxxxx.ID == 0)
{
dal.AddToXXXXXX(xxxxxxx);
}
try
{
dal.SaveChanges();
return true;
}
catch
{
return false;
}