locked
Not able to set a link to related entity. RRS feed

  • Question

  • Hi,
    I am trying to insert a record in the database, which is throwing a foreign key error.

    transaction.ACCOUNT.TRANSACTIONs.Add(transaction);

     

    this.context.AttachTo("ACCOUNT", transaction.ACCOUNT);
    this.context.AddLink(transaction.ACCOUNT, "TRANSACTIONs", transaction);
    this.context.SaveChanges(); // error is at this line

    When i say savechanges , it throws an error saying
    <message xml:lang="en-US">An error occurred while processing this request.</message>
     <innererror>
       <message>Entities in 'RI_BANKEntities.TRANSACTIONs' participate in the 'FK_TRANSACTION_ACCOUNT' relationship. 0 related 'ACCOUNT' were found. 1 'ACCOUNT' is expected.</message>

    Please suggest me a possible solution.

    Thanks,
    Avani

    Wednesday, September 2, 2009 12:05 PM

Answers

  • Try adding a link from the transaction to the account.


    this.context.AttachTo("ACCOUNT", transaction.ACCOUNT);
    this.context.AddLink(transaction.ACCOUNT, "TRANSACTIONs"
    , transaction);
    this.context.SetLink(transaction, "Account", transaction.Account);
    this.context.SaveChanges();

    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    • Marked as answer by Aavani Friday, September 4, 2009 4:15 AM
    Wednesday, September 2, 2009 4:54 PM
    Moderator

All replies

  • Try adding a link from the transaction to the account.


    this.context.AttachTo("ACCOUNT", transaction.ACCOUNT);
    this.context.AddLink(transaction.ACCOUNT, "TRANSACTIONs"
    , transaction);
    this.context.SetLink(transaction, "Account", transaction.Account);
    this.context.SaveChanges();

    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    • Marked as answer by Aavani Friday, September 4, 2009 4:15 AM
    Wednesday, September 2, 2009 4:54 PM
    Moderator
  • Thanks Phani for taking time and replying to my query :)


    Avani
    Friday, September 4, 2009 4:16 AM