Ask a questionAsk a question
 

AnswerComo fazer update em campos fk de um registro?

  • Tuesday, November 03, 2009 8:52 PMDaniel Ianegitz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?

Answers

  • Wednesday, November 04, 2009 8:17 AMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

All Replies

  • Wednesday, November 04, 2009 8:17 AMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Thursday, November 12, 2009 7:58 PMDaniel Ianegitz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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);