locked
Como fazer update em campos fk de um registro? RRS feed

  • Question

  • Amigos,

    Gostaria de dar update em campos FK da tabela produtos (campos Grupo, SubGrupo, Familia)

    Diretamente pelo SQL Server eu troco os registros FK por outros (desde que os outros existam nas tabelas pai).

    Mas quando tento utilizando LINQ to Entities, caio na seguinte exception:

    A propriedade 'codFamilia' faz parte das informações de chave do objeto e não pode ser modificada.

    Ps: A mensagem é em português mesmo, estou utilizando a versão traduzida do sql server 2008...Um pouco ruim mas a empresa que decidiu...

    Alguem sabe como fazer o update em campos que são FK?
    Tuesday, November 3, 2009 8:52 PM

Answers

  • I had to use google translator, so I hope I understood correctly ;-)

    When you change an existing entity, you can change its scalar values and references, but you cannot change its id (key) fields.

    For example, for existing db entities :
    person.Name = "David" - ok
    person.ID = 7 -not ok

    This is becaus the ObjectStateManager uses the ID information to keep track of entities in the context, and changing the ID of an existing entity contradicts EF behavior.

    There is no problem doing the following, though:

    person.Father = person2;

    If this doesn't answert your question, please paste the code you use that gave you the error (how you change the FK and how you save changes).

    Ido.

    Please mark posts as answers/helpful if it answers your question
    • Proposed as answer by haberman michael Wednesday, November 4, 2009 10:54 AM
    • Marked as answer by Yichun_Feng Wednesday, November 11, 2009 1:28 AM
    Wednesday, November 4, 2009 8:17 AM

All replies

  • I had to use google translator, so I hope I understood correctly ;-)

    When you change an existing entity, you can change its scalar values and references, but you cannot change its id (key) fields.

    For example, for existing db entities :
    person.Name = "David" - ok
    person.ID = 7 -not ok

    This is becaus the ObjectStateManager uses the ID information to keep track of entities in the context, and changing the ID of an existing entity contradicts EF behavior.

    There is no problem doing the following, though:

    person.Father = person2;

    If this doesn't answert your question, please paste the code you use that gave you the error (how you change the FK and how you save changes).

    Ido.

    Please mark posts as answers/helpful if it answers your question
    • Proposed as answer by haberman michael Wednesday, November 4, 2009 10:54 AM
    • Marked as answer by Yichun_Feng Wednesday, November 11, 2009 1:28 AM
    Wednesday, November 4, 2009 8:17 AM
  • Thanks Ido.

    My problem its to update some fk fields of a register.

    I solve the issue using this way:

    regToUpdate.TableReference.EntityKey = new EntityKey("EntitiesNamespace",
                                                                                "FKFieldName",
                                                                                valueToUpdate);
    Thursday, November 12, 2009 7:58 PM