0

I have the following:

    public int CreateExercise(Exercise newExercise) {

        db.Exercises.Add(newExercise);
        db.SaveChanges();
        return newExercise.ExerciseId;
    }

Where Exercise is the following:

public class Exercise {

    public Exercise() {
        Users = new Collection<User>();
    } 

    [Key]
    [Required]
    public int ExerciseId { get; set; }

    [StringLength(300, ErrorMessage = "The value cannot exceed 300 characters. ")]
    public string Title { get; set; }

    public virtual ICollection<User> Users{ get; set; }
 }

When I create a new Exercise, sometimes pre-existing Users come with it, Id included. Even with an existing UserId specified for these Users, new User entries are getting added here. I only want to add associations, not new users - I'd imagine EF is smart enough to figure this out? What am I doing wrong?

I'm just trying to avoid having to loop through all Users attached to a new Exercise, clearing them out before Adding, making sure they exist, adding the exercise, and then adding each individual user, finally calling SaveChanges.

SB2055
  • 12,272
  • 32
  • 97
  • 202
  • How is the `Exercise` instance being created and initialized? EF won't just add related items to a new entity unless something you do causes it. – Andrew Barber Jun 24 '14 at 20:51
  • @AndrewBarber it's being serialized from the client, along with associated, pre-existing users – SB2055 Jun 24 '14 at 20:53
  • 1
    Ahh; I misunderstood you. I don't have it on-hand, but that's a problem with doing the associations like that. You are doing an `Add()`, so unless you tell EF something different, it assumes the whole object graph needs to be added as new items to the database - if those other entities are not under tracking. There's something you do to indicate the related records are not to be added, but simply associated... don't remember offhand... – Andrew Barber Jun 24 '14 at 20:56

1 Answers1

2

When disconnected entities are reattached they are in Added state by default.

You can change the state after attaching the exercise.

public int CreateExercise(Exercise newExercise) {

    db.Exercises.Add(newExercise);

    // set all attached users to unchanged.
    db.ChangeTracker.Entries<User>().ToList().ForEach(p => p.State = EntityState.Unchanged);

    db.SaveChanges();
    return newExercise.ExerciseId;
}
codeworx
  • 2,715
  • 13
  • 12