none
Updating foreign key value in LINQ RRS feed

  • Question

  • Hi,

    I am using VB.NET with LINQ. I have 2 tables TableA and TableB.   TableA is linked to TableB with a Foreign Key column LinqID.  No I need to update the value of this column in TableA. The usual update statement throws a  ForeignKeyReferenceAlreadyHasValueException when trying to assign value directly

    So how exactly do I need to perform an Update statement give  ID field value in TableA  and LinqID field value connecting to TableB

    Thanks in advance
    Tuesday, January 26, 2010 10:40 PM

Answers

  • So what you have here are two properties on TableA - a navigation property of type TableB and the underlying foreign key ID.

    You can't change the underlying foreign key ID once you have caused the TableB property to be loaded as they would be out of sync.

    The best thing to do here would be to set the TableB property itself which will cause the underlying foreign key to be updated for you. 

    [)amien
    Wednesday, January 27, 2010 5:31 AM
    Moderator
  • You can find LINQ to SQL documentation on MSDN but I don't think there are any specific topics on this.

    It comes down to you set the navigation property instead of the FK.

    [)amien
    Wednesday, January 27, 2010 5:06 PM
    Moderator

All replies

  • Any one has any idea?
    Wednesday, January 27, 2010 3:08 AM
  • So what you have here are two properties on TableA - a navigation property of type TableB and the underlying foreign key ID.

    You can't change the underlying foreign key ID once you have caused the TableB property to be loaded as they would be out of sync.

    The best thing to do here would be to set the TableB property itself which will cause the underlying foreign key to be updated for you. 

    [)amien
    Wednesday, January 27, 2010 5:31 AM
    Moderator
  • Thanks for the reply. Can I get any reference or documentation I have been on the web for long with out a lead.
    Wednesday, January 27, 2010 2:28 PM
  • You can find LINQ to SQL documentation on MSDN but I don't think there are any specific topics on this.

    It comes down to you set the navigation property instead of the FK.

    [)amien
    Wednesday, January 27, 2010 5:06 PM
    Moderator