3

enter image description here

Using the model above, I am trying to save a new PamNewsMessage and add Tags to it then save it to the DB. However, after trying many different combinations of adding objects and saving, I still have the same problem with duplicate entries (not duplicate keys).

enter image description here

Currently, this is what I am using to make the changes, and it works for the PamNewsMessage, however if a Tag already exists, it makes a duplicates.

_theService = new WCFPamUpdateServiceEntities3();
_theService.PamNewsMessages.AddObject(pnm);
_theService.SaveChanges();

I am not sure what else I need to do here, it should be pretty straight forward. I have read a lot of people having similar problems with no clear fix that I have found. Can anybody help me out here I have messed around with this for many hours now reading and trying different combinations of ways to save things, changing states, and what not.

Thanks,

Richard

Sorry about not being able to post pictures, long time reader first time poster :-)
(fixed this for you - marc_s)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

Here is little bit theory when I met the problem first time. Here is some description related to EF.

Short answer is: EF will not do this for you.

You worked with detached objects and now you must explicitly tell EF what changes you did = it generally means playing with state of every entity in the graph and every independent association in the graph (many-to-many is always independent association). If you call AddObject you are telling EF to add whole object graph not only the single entity.

If you just know that you are adding NewsMessage and this message will have only existing Tags you can do something like:

_theService = new WCFPamUpdateServiceEntities3();
_theService.PamNewsMessages.AddObject(pnm);
foreach (var tag in pnm.Tags)
{
    _theService.ObjectStateManager.ChangeObjectState(tag, EntityState.Unchanged);
}
_theService.SaveChanges();

Obviously once you go to more complicated scenarios where you can also add new tags or remove connections to existing tags this simple solution will not work.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Yea, I have already been to the above sample that you created and the problem still persists when adding a new tag. Basically on creation of a message the user has the option to add tags from the existing list, or create new tags. Maybe I could add new tags with a separate call, then save the message using this method. – Richard Perryman Sep 13 '11 at 13:42
  • In such case you must check existence of each tag in database and it would make everything more complicated because you cannot have two instances of the same tag attached to the context. – Ladislav Mrnka Sep 13 '11 at 13:47
  • What about something like this.. if (tag exists in context) _theService.ObjectStateManager.ChangeObjectState(tag, EntityState.Unchanged); else _theService.ObjectStateManager.ChangeObjectState(tag, EntityState.Added); – Richard Perryman Sep 13 '11 at 14:02
  • It depends what you meant by `if(tag exists in context)`. You cannot have used tag loaded in context otherwise `AddObject` will throw exception and once you call `AddObject` you will not get Tag from database. – Ladislav Mrnka Sep 13 '11 at 14:08