0

I am getting the following error inside my asp.net mvc web application, when trying to edit an object:-

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

The Post Edit action method is:-

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ServerJoin sj, FormCollection formValues)
        {
            string controllername = RouteData.Values["controller"].ToString();
            var databaseTechnology = repository.FindTechnology(sj.Server.RackID);

                    //code goes here

 repository.InsertOrUpdateServer(sj.Server, User.Identity.Name, sj.Resource.RESOURCEID);

                        repository.Save();
                        return RedirectToAction("Details", new { id = sj.Server.TMSServerID });
           //codegoes here
        }

The Repository method that perform the Edit and which is causing the exception is:-

public void InsertOrUpdateServer(TMSServer server, string username,long assetid)
        {
            var resource = GetResourceDetials(assetid);
            if (server.TMSServerID == default(int))
            {
                // New entity
               //codegoes here
            else
            {


                var auditinfo = IntiateTechnologyAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "EDIT").ID,
              tms.TechnologyTypes.SingleOrDefault(a => a.Name.ToUpper() == "SERVER").AssetTypeID,
              username, server.TMSServerID);
                server.IT360SiteID = resource.SITEID.Value;

                tms.Entry(server).State = EntityState.Modified;
                InsertOrUpdateTechnologyAudit(auditinfo);

            }
        }

The reporsitory method for the Audit 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;
            }
        }

The model class for the TMSServer is:-

public partial class TMSServer
    {
        public 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<TMSVirtualMachine> TMSVirtualMachines { get; set; }
    }

The model class for the technology is :-

public partial class Technology
    {
        public Technology()
        {
            this.TMSSwitchPorts = new HashSet<TMSSwitchPort>();
            this.TechnologyAudits = new HashSet<TechnologyAudit>();
            this.TechnologyIPs = new HashSet<TechnologyIP>();
        }

        public int TechnologyID { get; set; }
        public string Tag { get; set; }
        public bool IsDeleted { get; set; }
        public byte[] timestamp { get; set; }
        public Nullable<int> TypeID { get; set; }
        public Nullable<System.DateTime> StartDate { get; set; }
        public Nullable<long> IT360ID { get; set; }

        public virtual TMSFirewall TMSFirewall { get; set; }
        public virtual TMSRack TMSRack { get; set; }
        public virtual TMsRouter TMsRouter { get; set; }
        public virtual TMSServer TMSServer { get; set; }
        public virtual TMSStorageDevice TMSStorageDevice { get; set; }
        public virtual TMSSwitch TMSSwitch { get; set; }
        public virtual ICollection<TMSSwitchPort> TMSSwitchPorts { get; set; }
        public virtual TechnologyType TechnologyType { get; set; }
        public virtual ICollection<TechnologyAudit> TechnologyAudits { get; set; }
        public virtual ICollection<TechnologyIP> TechnologyIPs { get; set; }
        public virtual TMSVirtualMachine TMSVirtualMachine { get; set; }
    }

I am getting the error on the tms.Entry(server).State = EntityState.Modified; inside the InsertOrUpdateServer repository method. Any idea what is causing this exception ?, baring in mind that i am only tracking a single TMSServer object.

Thanks in advance for any help.

  • Have you seen this: http://stackoverflow.com/questions/12585664/an-object-with-the-same-key-already-exists-in-the-objectstatemanager-the-object or this http://stackoverflow.com/questions/5672255/an-object-with-the-same-key-already-exists-in-the-objectstatemanager-the-object – David Tansey Aug 14 '13 at 12:51
  • i do not want to start adding extra code, i always use the same approach for editing objects and it worked just fine. so i am trying to figure out what is causing the problem in my current code, maybe i can find a simpler solution... – Asp.netmvc Asp.netmvc Aug 14 '13 at 13:02
  • @DavidTansey in addition to that both examples havve different problem, as they track multiple objects , but in my case i am only tracking single object.. – Asp.netmvc Asp.netmvc Aug 14 '13 at 14:14

0 Answers0