0

I have two classes that I have simplified for this SO question. Whenever I add a new 'parent' and attach existing 'child' entities, I end up with duplicated children. What am I doing wrong?

 public class Group
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int GroupId { get; set; }
    public virtual Child ChildOne { get; set; }
    public virtual Child ChildTwo { get; set; }
}

public class Child
{
    [Key]
    public int ChildId { get; set; }
    public bool Available { get; set; }
}
using (var context = new RPSContext())
{
    Child childOne = context.Child.Where(p => p.Available == true).OrderBy(p => p.ChildId).FirstOrDefault();
    Child childTwo = context.Child.Where(p => p.Available == true).OrderBy(p => p.ChildId).Skip(1).Take(1).FirstOrDefault();
}
Parent parent = new Models.Parent;
parent.ChildOne = childOne;
parent.ChildTwo = childTwo;

using (var context = new RPSContext())
        {
            context.Parent.Add(parent);
            parent.ChildOne.Available = false;
            parent.ChildTwo.Available = false;
            context.SaveChanges();
        }

I hope I didn't make any errors when I simplified this code. Can anyone assist?

12
  • I guess by Parent you mean Group. And you get two new Child records created? Commented Dec 19, 2016 at 15:52
  • Yes, it is really a one to many relationship. Parent/Child is the wrong term, but I do get multiples of the children objects. Commented Dec 19, 2016 at 16:29
  • I'm wondering if there is some context nonsense going on here. Can you refactor this so you only have one new RPSContext? Commented Dec 19, 2016 at 16:57
  • I tried that with the same result. Commented Dec 19, 2016 at 16:58
  • 1
    Thank you to Ivan and Developer. Attaching is what I needed to do. Not sure if I can give you credit since you just added a comment, but if you want to add an answer I will go ahead and accept it. Thanks again! Commented Dec 19, 2016 at 17:16

1 Answer 1

1

Try performing in single context. You are fetching childOne and childTwo in one context which is disposed immediately after fetching it.

So you might need to attach these two child entities in the new context context.Child.Attach(childOne);context.Child.Attach(childTwo‌​);

OR perform both operations in the same context so that child entities gets related instead of getting added

Sign up to request clarification or add additional context in comments.

3 Comments

I found that combining into one context had no impact, but attaching did. Thanks for your help!
@AbrahamLincoln did you omit a .AsNoTracking from the code you copied for this question? If combining into a single context didn't fix it, then you're loading the Child entities in a way that's different from "normal"
@Vlad274 - thats true.. something that works with attach should also work in the same context IMO unless tracking is explicitly disabled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.