【问题标题】:Update Foreign key (entity) with entity framework V1使用实体框架 V1 更新外键(实体)
【发布时间】:2009-10-03 07:13:24
【问题描述】:

我有一个带有以下实体对象的小型 ASP.NET MVC 应用程序:

  • 人员标识
  • 名称(字符串)
  • 名字(字符串)
  • 国家(国家)

国家

  • 国家标识
  • 姓名

我可以添加和删除实体,这很好用。我也可以更新名字,名字。 但是我如何用另一个国家/地区更新国家/地区属性。

我在尝试

p.Country = (from c in db.Country 
             where c.CountryId == countryId 
             select c).First();

但这会引发异常{“ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。”}”

甚至在我对数据上下文调用 SaveChanges 之前。

谁能解释我如何更新这个属性?

亲切的问候 迪特

【问题讨论】:

    标签: c# asp.net-mvc entity-framework


    【解决方案1】:

    db 是你的上下文吗?你应该能够做到:

    p.Country = ctx.Country.First(c => c.CountryId == countryId);
    

    或者,如果您不想查询数据库以获取外键实体,您也可以使用 EntityKey 来达到相同的效果:

    p.CountryReference.EntityKey = new EntityKey("MyDb.Country", "CountryId", countryId);
    

    【讨论】:

    • db 是我的内容,但是您的解决方案不起作用,我在 p.Country = db.Country.First... {"已存在具有相同键的对象在 ObjectStateManager 中。ObjectStateManager 无法使用相同的键跟踪多个对象。"}
    【解决方案2】:

    更新导航属性时,与此类似的代码对我有用。

       Country country = new Country{CountryId = theNewCountryID };
        db.AttachTo("Countries", country);
        p.Country = country;
        db.Savechanges();
    

    创建新国家的存根,然后将其附加到国家实体集,将新国家分配给实体的导航国家属性并调用 SaveChanges()。 在 AttachTo 调用中使用您所在国家/地区的 EntitySet 名称。

    【讨论】:

      【解决方案3】:

      这对我有用。在模型中:

      <%= Html.Textbox("Country.CountryId", Model.Countries) %> // I'm using a form model view
      

      在控制器中:

      Person originalPerson = (from p in db.PersonSet
                              where p.PersonId == updatedPerson.PersonId
                              select p).First();
      
      Country country = (from c in db.CountrySet 
                        where c.CountryId == updatePerson.Country.CountryId 
                        select c).First();
      
      db.Attach(country);
      originalPerson.Country = country;
      db.ApplyPropertyChanges(originalPerson.EntityKey.EntitySetName, updatedPerson);
      db.Savechanges();
      

      【讨论】:

        【解决方案4】:

        我使用了第二种解决方案,没有任何异常,但是即使在我调用 AcceptChanges 之后,数据库中的 CountryId 也没有更改。

        【讨论】:

        • 您好 Dav,您的代码适用于新国家/地区,我想使用其他现有国家/地区更新国家/地区属性。但无论如何,还是要考虑节食者
        • @Deiter,在 @theNewCountryID 中将现有国家/地区的 ID 放入要将 Person 记录更新到的国家/地区表中。也许这是一个错误的变量名选择!
        • 仍然出现异常 - {“ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。”} System.Exception {System.InvalidOperationException} 这是正常的因为我附上了对象 statemanager 已经知道的国家/地区
        • public void UpdateRSVP2(GuestResponse updatedResponse, int countryId) { updatedResponse.EntityKey = (来自 db.GuestResponse 中的响应,其中 response.GuestResponseId == updatedResponse.GuestResponseId 选择响应).FirstOrDefault().EntityKey; updatedResponse.Country = (from c in db.Country where c.CountryId == countryId select c).First(); db.ApplyPropertyChanges(updatedResponse.EntityKey.EntitySetName, updatedResponse); db.SaveChanges();
        • 这是我更新导航属性的方式。如果您没有任何要更新的标量属性,您可以跳过 ApplyPropertyChanges 调用。 origResponse = (from r in db.GuestResponse where r.GuestResponseId == updatedResponse.GuestResponseId select r).FirstOrDefault();国家国家 = 新国家 { CountryID = countryId }; db.AttachTo("CountriesSet", country); origResponse.Country = country db.ApplyPropertyChanges("GuestResponseSet", updatedResponse); db.SaveChanges();
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多