我有以下负责更新对象的方法:-
private TMSEntities tms = new TMSEntities();
//code goes here...
public void InsertOrUpdateServer(TMSServer server, string username,long assetid)
{
// code goes here
}
else
{
var auditinfo = IntiateAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "EDIT").ActionID,
tms.TechnologyTypes.SingleOrDefault(a => a.Name.ToUpper() == "Server").AssetTypeID,
username, server.ServerID);
server.IT360SiteID = resource.SITEID.Value;
tms.Entry(server).State = EntityState.Modified;
InsertOrUpdateAudit(auditinfo);
}
}
但我收到以下错误:-
tms.Entry(server).State = EntityState.Modified;
ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidOperationException:ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。
源错误:
虽然我只跟踪一个对象,但我该如何解决这个问题?
更新 我在我的方法中添加了以下内容:-
tms.Entry(server).CurrentValues.SetValues(server);
代替
tms.Entry(server).State = EntityState.Modified;
但我得到以下例外: -
无法为“TMSServer”类型的实体调用成员“CurrentValues”,因为该实体在上下文中不存在。要将实体添加到上下文中,请调用 DbSet 的 Add 或 Attach 方法。
第二次更新:-
public Audit IntiateAudit(int actionId, int assettypeID, string username, int? technologyID)
{
TechnologyAudit ta = new TechnologyAudit();
ta.ActionID = actionId;
ta.AssetTypeID = assettypeID;
ta.DateTimeStart = DateTime.Now;
ta.UserName = username;
if (technologyID != null)
{
ta.TechnologyID = technologyID.Value;
}
return ta;
}
&
public void InsertOrUpdateAudit(Audit ta)
{
if (ta.ID == default(int))
{
// New entity
ta.DateTimeEnd = DateTime.Now;
tms.TechnologyAudits.Add(ta);
}
else
{
// Existing entity
tms.Entry(ta).State = EntityState.Modified;
}
&
public partial class TechnologyType
{
public TechnologyType()
{
this.SecurityroleTypePermisions = new HashSet<SecurityroleTypePermision>();
this.Technologies = new HashSet<Technology>();
this.TechnologyAudits = new HashSet<TechnologyAudit>();
}
public int AssetTypeID { get; set; }
public string Name { get; set; }
public virtual ICollection<SecurityroleTypePermision> SecurityroleTypePermisions { get; set; }
public virtual ICollection<Technology> Technologies { get; set; }
public virtual ICollection<TechnologyAudit> TechnologyAudits { get; set; }
}
&
public partial class TMSServer
{
public TMSServer()
{
this.TMSServers1 = new HashSet<TMSServer>();
this.TMSVirtualMachines = new HashSet<TMSVirtualMachine>();
}
public int TMSServerID { get; set; }
public Nullable<int> ServerModelID { get; set; }
public int DataCenterID { get; set; }
public string ILOIP { get; set; }
public int RackID { get; set; }
public Nullable<int> StatusID { get; set; }
public Nullable<int> BackUpStatusID { get; set; }
public int RoleID { get; set; }
public Nullable<int> OperatingSystemID { get; set; }
public Nullable<int> VirtualCenterID { get; set; }
public string Comment { get; set; }
public byte[] timestamp { get; set; }
public long IT360SiteID { get; set; }
public virtual DataCenter DataCenter { get; set; }
public virtual OperatingSystem OperatingSystem { get; set; }
public virtual ServerModel ServerModel { get; set; }
public virtual Technology Technology { get; set; }
public virtual TechnologyBackUpStatu TechnologyBackUpStatu { get; set; }
public virtual TechnologyRole TechnologyRole { get; set; }
public virtual TechnologyStatu TechnologyStatu { get; set; }
public virtual TMSRack TMSRack { get; set; }
public virtual ICollection<TMSServer> TMSServers1 { get; set; }
public virtual TMSServer TMSServer1 { get; set; }
public virtual ICollection<TMSVirtualMachine> TMSVirtualMachines { get; set; }
}
} }
第三次更新
审核代码是:-
public TechnologyAudit IntiateTechnologyAudit(int actionId, int assettypeID, string username, int? technologyID)
{
TechnologyAudit ta = new TechnologyAudit();
ta.ActionID = actionId;
ta.AssetTypeID = assettypeID;
ta.DateTimeStart = DateTime.Now;
ta.UserName = username;
if (technologyID != null)
{
ta.TechnologyID = technologyID.Value;
}
return ta;
}
public void InsertOrUpdateTechnologyAudit(TechnologyAudit ta)
{
if (ta.ID == default(int))
{
// New entity
ta.DateTimeEnd = DateTime.Now;
tms.TechnologyAudits.Add(ta);
}
else
{
// Existing entity
tms.Entry(ta).State = EntityState.Modified;
}
}