0

オブジェクトを編集しようとすると、asp.net mvc Web アプリケーション内で次のエラーが発生します。

同じキーを持つオブジェクトが ObjectStateManager に既に存在します。ObjectStateManager は、同じキーを持つ複数のオブジェクトを追跡できません。

投稿編集アクション メソッドは次のとおりです。

[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
        }

編集を実行し、例外を引き起こしているリポジトリ メソッドは次のとおりです。

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);

            }
        }

監査の記録方法は次のとおりです。

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;
            }
        }

TMSServer のモデル クラスは次のとおりです。

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; }
    }

テクノロジーのモデル クラスは次のとおりです。

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; }
    }

tms.Entry(server).State = EntityState.Modified;InsertOrUpdateServer リポジトリ メソッドの内部でエラーが発生してい ます。この例外の原因は何ですか?、私は単一の TMSServer オブジェクトのみを追跡していることを念頭に置いてください。

助けてくれてありがとう。

4

0 に答える 0