我有一个ObjectDataSource
调用业务层方法来更新 EF 并保存。我有这个奇怪的问题。以下是 ODS 标记:
<asp:ObjectDataSource ID="odsDeviceForm" runat="server" ypeName="Spectre.BLL..DeviceManager"
DataObjectTypeName="Model.Device" SelectMethod="GetById"
InsertMethod="Insert" oninserting="OnItemInserting" UpdateMethod="UpdateEx"
oninserted="OnItemInserted" onupdated="OnItemUpdated"
onupdating="OnItemUpdating" onobjectcreated="OnOdsObjectCreated"
onselected="OnOdsItemSelected" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="orig{0}" >
<SelectParameters>
<asp:SessionParameter Name="primaryKey" SessionField="SelectedDeviceId" Type="Int32" />
</SelectParameters>
以下是 BLL 和 repositoryCode: BLL:
public void UpdateEx(T entity, T origentity)
{
try
{
repository.Update(origentity, entity);
repository.Save();
}
catch (Exception)
{
throw;
}
}
存储库:
public void Update(T orig, T newEntity)
{
myContext.Entry<T>(newEntity).CurrentValues.SetValues(newEntity);
}
当此代码运行时,我得到一个异常:
不能为“设备”类型的实体调用成员“当前值”,因为该实体在上下文中不存在。要将实体添加到上下文中,请调用 DbSet 的 Add 或 Attach 方法。
但是,如果我执行 dbset.attach,那么我会得到实体已经存在的异常。
我不知道我能做什么,对我来说似乎是一个循环问题。我已经秃了半秃了。请帮忙。