locked
Add object with association RRS feed

  • Question


  • I can not add an object with associations. I have to add object then bind associations. otherwise I get an error like both or one side has added state. because of unique key restrictions sometimes belove workaround is hopeless. i can not add new object with null values.

    is it bug or naturel behaviour of EF?

    Code Snippet

    DistraintRequestAccountSearchResult item = new DistraintRequestAccountSearchResult();
    db.AddObject("DistraintRequestAccountSearchResultSet", item);                   
    db.SaveChanges(); //I have to save object with null associations
    item.Account = account;
    db.AddBinding(item, "Account", account);
    item.DistraintRequestItem = distraintRequestItem;
    db.AddBinding(distraintRequestItem, "DistraintRequestAccountSearchResult", item);
    distraintRequestItem.DistraintRequestAccountSearchResult.Add(item);
    db.UpdateObject(distraintRequestItem);                   
    db.SaveChanges();

    Tuesday, March 11, 2008 6:43 AM

Answers

  • You are correct. This was a problem with the CTP bits. As KSid said earlier, we will releasing a batching feature with our beta1 bits that will help you solve this problem.

     

    Thanks

    Pratik

     

    Thursday, March 13, 2008 6:40 PM
    Moderator

All replies

  •  

    Have you tried removing the first call to SaveChanges? That should allow you to call SaveChanges after doing the binding but the December CTP still sends the commands to the Data Service as three seperate requests. One with the new item and null values for the foreign keys and then an update for each call to AddBinding.

     

    The only work around at the moment is to set the foreign key fields to nullable. If I remember correctly, the next public release will allow us to send batch requests so this should no longer be an issue.

    Tuesday, March 11, 2008 11:30 AM
  • when i remove first savechanges i get error that i mentioned in first message.
    Tuesday, March 11, 2008 11:46 AM
  •  

    Oops. Sorry, bad info. I've just done a test and I get a similar (if not the same error) as you. As I mentioned earlier, I think it's something that will be resolved in the next public release but maybe someone else will be able to confirm.
    Tuesday, March 11, 2008 12:22 PM
  • You are correct. This was a problem with the CTP bits. As KSid said earlier, we will releasing a batching feature with our beta1 bits that will help you solve this problem.

     

    Thanks

    Pratik

     

    Thursday, March 13, 2008 6:40 PM
    Moderator
  • When will you release this beta ?

     

    I've the same bug :-)

    Thursday, March 27, 2008 6:58 PM
  •  

    I can't find the exact thread at the moment but if I remember correctly, it is due sometime in April.
    Friday, March 28, 2008 9:03 AM
  • OK. Thanks.
    Wednesday, April 2, 2008 6:24 PM