简体   繁体   English

实体框架-使用导航属性编辑多个实体

[英]Entity Framework - Edit multiple entities with navigation properties

I've been looking for a solution for my problem since a couple days. 几天以来,我一直在寻找解决方案。 I'm relatively new to entity framework and just getting used to it. 我对实体框架比较陌生,并且刚刚习惯它。

Here is my problem: 这是我的问题:

I made an Web API in ASP.NET MVC. 我在ASP.NET MVC中制作了一个Web API。 In one method in the API I need to update multiple existing records (same type of entity). 在API的一种方法中,我需要更新多个现有记录(相同类型的实体)。 The entity has the following structure: 该实体具有以下结构:

Appointment 约定

  • GUID (PK) GUID(PK)
  • Name (Name of the Appointment for a interface) 名称(接口的约会名称)
  • Team_Creation_FK Team_Creation_FK
  • Team_Editing_FK Team_Editing_FK
  • Team_Delivery_FK Team_Delivery_FK
  • Team_Creation (Navigation Property) Team_Creation(导航属性)
  • Team_Editing (Navigation Property) Team_Editing(导航属性)
  • Team_Delivery (Navigation Property) Team_Delivery(导航属性)

This entity can contain the same teams in different sections (creation, editing, creation). 该实体可以在不同部分(创建,编辑,创建)包含相同的团队。 As example the columns Team_Creation and Team_Editing can contain the same Team (Team: Test (for example)). 例如,“ Team_Creation”和“ Team_Editing”列可以包含相同的“团队”(例如,团队:测试)。

So my method to update multiple records looks like this: 因此,我更新多个记录的方法如下所示:

    public IHttpActionResult EditAppointment(IEnumerable<Appointment> appointments)
    {
        foreach(Appointment appointment in appointments)
        {
            appointment.Team_Creation_FK = appointment.Team_Creation != null ? appointment.Team_Creation.GUID: (Guid?)null;                
            appointment.Team_Editing_FK = appointment.Team_Editing != null ? appointment.Team_Editing.GUID: (Guid?)null;
            appointment.Team_Delivery_FK = appointment.Team_Delivery != null ? appointment.Team_Delivery.GUID: (Guid?)null;           

            db.Entry(appointment).State = EntityState.Modified;                
        }

        db.SaveChanges();

        return Ok();
    }

But when I try to update entities with the same team in different entities I get the following error: 但是,当我尝试在不同实体中与同一团队的实体进行更新时,出现以下错误:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code. EntityFramework.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理。 Additional information: Attaching an entity of type 'Orderus.Models.Team' failed because another entity of the same type already has the same primary key value. 附加信息:附加类型为“ Orderus.Models.Team”的实体失败,因为相同类型的另一个实体已经具有相同的主键值。 This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. 如果图形中的任何实体具有相互冲突的键值,则使用“附加”方法或将实体的状态设置为“不变”或“修改”时,可能会发生这种情况。 This may be because some entities are new and have not yet received database-generated key values. 这可能是因为某些实体是新实体,尚未收到数据库生成的键值。 In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate. 在这种情况下,请使用“添加”方法或“已添加”实体状态来跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改”。

Why does it attach the navigation property to the dbContext? 为什么将导航属性附加到dbContext? Or am I looking in a wrong place? 还是我看错地方了? how I can fix this problem? 我该如何解决这个问题?

Thanks for your help, I appreciate any kind of help! 感谢您的帮助,感谢您的帮助!

Update: 更新:

Class Appointment 班级任命

public partial class Appointment
{
    public System.Guid GUID { get; set; }
    public string Name{ get; set; }
    public Nullable<System.Guid> Team_Creation_FK { get; set; }
    public Nullable<System.Guid> Team_Editing_FK { get; set; }
    public Nullable<System.Guid> Team_Delivery_FK { get; set; }

    public virtual Team Team_Creation { get; set; }
    public virtual Team Team_Editing { get; set; }
    public virtual Team Team_Delivery { get; set; }
}

Assuming appointment entities aren't new and teams already exists, the problem is that you have many instances of Team object with the same I'd. 假设约会实体不是新的并且团队已经存在,问题是您有许多Team对象实例,它们具有相同的名称。 They represent the same Team, but are different instances. 他们代表相同的团队,但是是不同的实例。

So when you attach an appointment, EF attaches all object graph and find N Team objects with the same Id. 因此,当您附加约会时,EF会附加所有对象图并找到具有相同ID的N个Team对象。

Solution: set appointment.Team_xx=null after setting the corresponding team Guid. 解决方案:设置相应的团队Guid后,设置约会。Team_xx= null。 Or even better, just post appointment with team guids instead of full graph and save network traffic. 甚至更好的是,只需使用团队指导而不是完整的图形发布约会,即可节省网络流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM