0

I'm using Entity Framework 4.4 and I have a One-To-Many relationship model like this:

class A { 
   public int Id { get; set; }
   public virtual B { get; set; }
   public int BId;
}
class B {
   public Id { get; set; }
   private ICollection<A> _As;
   public virtual ICollection<A> As
   {
      get { return _As ?? (_As = new HashSet<A>()); }
      protected set { _As = value; }
   }
}

Now, supposing I have 2 A instances, both with the same B instance. Then I'd have something like this (JSON representation):

A: { Id: 1, BId: 1, B: { Id: 1, As: [ 1, 2 ] } }
A: { Id: 2, BId: 1, B: { Id: 1, As: [ 1, 2 ] } }

I want to then cycle through my current list of A instances and add the to the db context:

foreach (var a in LocalAList)
{ 
   dbContext.Add(a);
   dbContext.SaveChanges();
}

The problem is that I get exceptions informing me that I've inserted duplicate objects. I've looked around and found things dealing with Entity modification and attachment, but I'm still pretty new to EF. I would imagine that this is a typical thing and there's an idiom for handling object insertion where the object already exists.

How can I efficiently add new objects to the context while avoiding the addition of duplicates? Should I just capture the duplicate exception and ignore it? Is there a better way to check?

4
  • Are the Ids database generated identities? And what exception do you get exactly? An exception about a primary key constraint violation? (In that case you can't have database generated identities, I think...) What do you want to insert exactly? Only the As, or the Bs as well? Or are the Bs existing entities and you want only to create a relationship between new As and existing Bs? Commented Mar 5, 2013 at 18:16
  • See my edit. The Ids are NOT generated identities. Commented Mar 6, 2013 at 7:44
  • 1
    Honestly, I won't answer now anymore. For the sake of fairness to Mark Oreta's answer you should either roll back your edit to the former version and write a new question with your edit, or write a comment under Mark Oreta's answer that you have changed your question to give him a chance to adjust his answer. Your question change is so massive that his answer currently even doesn't make sense anymore. Commented Mar 6, 2013 at 17:51
  • Agreed @Slauma; see stackoverflow.com/questions/15255898/… Commented Mar 6, 2013 at 19:05

1 Answer 1

2

You could do a simple test to see if a already exists in the database, the simplest one is to see if the Id is 0. This is because once the A is added to the DB, an Id is given to your entity. At that point, you should just reattach it rather than re-adding it

Example:

foreach (var a in LocalAList)
{ 
  if (a.Id == 0)
    dbContext.Add(a);
   else 
    dbContext.As.Attach(a)

   dbContext.SaveChanges();
}
Sign up to request clarification or add additional context in comments.

Comments

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.