I have the following method which is responsible to update an object:-
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);
}
}
But I am getting the following error on the :-
tms.Entry(server).State = EntityState.Modified;
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
Source Error:
Although I am tracking only one object, so how I can solve this problem?
UPDATE I have added the following to my method:-
tms.Entry(server).CurrentValues.SetValues(server);
instead of
tms.Entry(server).State = EntityState.Modified;
but i am getting the following exception:-
Member 'CurrentValues' cannot be called for the entity of type 'TMSServer' because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet.
Second update:-
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; }
}
} }
Third UPDATE
the Audit code is :-
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;
}
}