更新:啊!哑误区:我有两个可用的存储库实例,而且,我们的实现要求我们为每个Repo提供一个连接字符串,所以两个Repos指向不同的数据库,我在一个数据库中添加一个实体到另一个数据库,可能无法在更新的数据库中找到它的ID。
我正在使用EF 5运行.Net MVC 4.0。我们实现了一个存储库模式。在WebApi控制器的以下Delete
方法中,我有以下代码:
int userID = UserHelper.GetCurrentUserID();
DateTime now = DateTime.UtcNow;
ExhibitLinkRepository el = new ExhibitLinkRepository();
el.setCase = caseID;
ExhibitLink link = el.All.SingleOrDefault(l => l.id == id);
//Mark Link Deleted
link.usermodified_id = userID;
link.datetimemodified = now;
link.deleted_flag = true;
exhibitLinkRepository.InsertOrUpdate(link);
exhibitLinkRepository.SaveChanges();
exhibitLinkRepository.InsertOrUpdate
:
public void InsertOrUpdate(ExhibitLink exhibitLink)
{
if (exhibitLink.id == default(int))
{
// New entity
context.ExhibitLinks.Add(exhibitLink);
}
else
{
// Existing entity
context.Entry(exhibitLink).State = EntityState.Modified;
}
}
当我调用context.SaveChanges()
时,我感到害怕:
存储更新,插入或删除语句会影响意外的行数(0)。自实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目。
现在,ExhibitLink表具有以下外键约束:
在某些相关表格的基础数据库上恰好有触发器,但禁用它们并没有改变结果。
我只是不明白。有什么想法吗?