none
Entity Framework Adding Multiple Objects Creating Foreign Key Error

    Question

  • I have a model that has three tables  Property,Details,Invoice.  Property has primary Key of Property ID and others have PropertyId as the Foreign key. The problem here is i have a invoice screen where i am adding multiple properties and not saving changes until whole invoice is saved. It works correctly over there but when i will submit the invoice it will give me this error 

    Unable to determine the principal end of the 'Model.FK_Details_Property' relationship. Multiple added entities may have the same primary key. 

     

    I have debugged the problem where each of them is having a state Added but its not generating primary key for PropertyId which is 0 for each property. I think this is the problem because of that. Dont know why it is not generating primary key when it has an state as ADDED. 

     I finally came up here to ask because not able to find any specific reason for this question. IF anybody faced this problem before or know how to deal with this please post your comments.

    Similar question can be found here

    Thanks in advance.


    Tuesday, November 01, 2011 2:41 PM

Answers

  • Got the Answer it was in the Entity Model only EDMX where i was supposed to have parent child tables but there was an extra reference creating circular dependency . Deleted it and it worked for me. Thanks for the reply by the way darnold924!
    • Marked as answer by Nivid Dholakia Tuesday, November 01, 2011 6:01 PM
    Tuesday, November 01, 2011 6:01 PM

All replies

  • On 11/1/2011 10:41 AM, Nivid Dholakia wrote:
    > I have a model that has three tables Property,Details,Invoice. Property
    > has primary Key of Property ID and others have PropertyId as the Foreign
    > key. The problem here is i have a invoice screen where i am adding
    > multiple properties and not saving changes until whole invoice is saved.
    > It works correctly over there but when i will submit the invoice it will
    > give me this error
    >
    > Unable to determine the principal end of the 'Model.FK_Details_Property'
    > relationship. Multiple added entities may have the same primary key.
    >
    > I have debugged the problem where each of them is having a state Added
    > but its not generating primary key for PropertyId which is 0 for each
    > property. I think this is the problem because of that. Dont know why it
    > is not generating primary key when it has an state as ADDED.
    >
    > I finally came up here to ask because not able to find any specific
    > reason for this question. IF anybody faced this problem before or know
    > how to deal with this please post your comments.
    >
    > Similar question can be found here
    >
     
    Are you sure the primary-key is an Identity field +1 on the table?  If
    it's not a +1 then all it's going to do is assign every object with the
    same key, instead of primary-key-indentity +1.
     
    Tuesday, November 01, 2011 4:36 PM
  • Yes because it works from another screen where i can add multiple properties. But the thing here is i am not saving the property if we save all the properties before the invoice is saved it will work.
    Tuesday, November 01, 2011 5:23 PM
  • Got the Answer it was in the Entity Model only EDMX where i was supposed to have parent child tables but there was an extra reference creating circular dependency . Deleted it and it worked for me. Thanks for the reply by the way darnold924!
    • Marked as answer by Nivid Dholakia Tuesday, November 01, 2011 6:01 PM
    Tuesday, November 01, 2011 6:01 PM
  • Hi Nivid;

    To your statement:
    I have debugged the problem where each of them is having a state Added but its not generating primary key for PropertyId which is 0 for each property. I think this is the problem because of that. Dont know why it is not generating primary key when it has an state as ADDED.
    When the database generates the Primary Key on a table the Entity Framework will assign a Zero for the Primary Key until you have called SaveChanges method of the ObjectContext then all the Primary Key's will have the value assigned by the DB available.

    How did you create the EF model?


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, November 01, 2011 6:06 PM