locked
Foreign Key Relationship RRS feed

  • Question

  • Hello,

     

    I have a simple question on the usage of the Foreign Key relationships in the linq designer and datacontext. Say I have:

     

    table Contact:

    -ContactID (int, PK)

    -AddressID (int, FK to Address.AddressID)

     

    table Address:

    -AddressID (int, PK)

    -AddressText (string)

     

    In the datacontext, I have both Contact and Address class. The Address class has AddressID, Address, and another property usually called Contacts, that is an IEnumerable of all the Contacts that use this Address. The Contact class has

    a ContactID property, a ShipToAddressID property, and a Address property that represents the foreign key constraint to the Address table.

     

    Now before going into my question, I will say that the purpose of LINQ in my application is a data access tier, so that from the client, there will be no data context. I am manually attaching everything that is to be added or updated to a new data context inside of a web service method.

     

    So with these foreign key 'properties', I know I can use them in a read only context (just retrieving the information). But say I retrieve a Contact, the client updates the Address (something like contact.Address.AddressText = "new value"). When I attempt to save that, it does not update, but throws an error saying a foreign key constraint failed. I have checked, and the AddressID in the Contact object is correct, and the AddressID of the Contact.Address object is valid, but it still doesn't work.

     

    Are the foreign key properties of the generated data context only to be used when retrieving data? Could I add an Address automatically by setting the address property of a Contact object? How would it know if it needed to create a new one if the AddressID is an integer, and thus defaults to 0? Can update be using in conjunction with them? Or does any changes have to be applied to the database without using the generated foreign key properties?

     

    Thanks for any help you can give me.

    -Brandon

    Friday, February 8, 2008 4:03 PM

Answers

  •  

    The problem may be how you are attaching the changed objects to the new DataContext.  You have to do an Attach call for each entity.  If you only Attach the 'contact' entity, the Address entity will not be considered changed and will not generate an update.  In order to tell the DataContext that an object was changed you have to provide the information about how it was changed, by using the Attach method that takes both an entity and its original state.
    Saturday, February 9, 2008 6:16 AM

All replies

  • Is the addressid genereated by identity in the database?

     

    If so you need to mark the column with IsDbGenerated, otherwise it will try to add the 0 as id for the address and the relation.

     

     

    Friday, February 8, 2008 9:16 PM
  •  

    The problem may be how you are attaching the changed objects to the new DataContext.  You have to do an Attach call for each entity.  If you only Attach the 'contact' entity, the Address entity will not be considered changed and will not generate an update.  In order to tell the DataContext that an object was changed you have to provide the information about how it was changed, by using the Attach method that takes both an entity and its original state.
    Saturday, February 9, 2008 6:16 AM
  • Thanks. This was the primary part of my problem-- I was not attaching each entity. I also had a bug of my own creation, but it had nothing to do with LINQ.

     

    Thanks,

    Brandon

     

    Monday, February 11, 2008 9:15 PM