简体   繁体   中英

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. In one method in the API I need to update multiple existing records (same type of entity). The entity has the following structure:

Appointment

  • GUID (PK)
  • Name (Name of the Appointment for a interface)
  • Team_Creation_FK
  • Team_Editing_FK
  • Team_Delivery_FK
  • Team_Creation (Navigation Property)
  • Team_Editing (Navigation Property)
  • Team_Delivery (Navigation Property)

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

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. 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. 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? 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. 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.

Solution: set appointment.Team_xx=null after setting the corresponding team Guid. Or even better, just post appointment with team guids instead of full graph and save network traffic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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