Pergunta

Eu tenho 2 classes com uma associação LINQ entre eles ou seja:.

Table1:       Table2:
ID            ID
Name          Description
              ForiegnID

A associação aqui é entre Table1.ID -> Table2.ForiegnID

Eu preciso ser capaz de alterar o valor de Table2.ForiegnID, no entanto eu não posso e acho que é por causa da associação (como quando eu removê-lo, ele funciona).

Por isso, alguém sabe como eu posso mudar o valor do campo associado Table2.ForiegnID?

Foi útil?

Solução

Confira o arquivo designer.cs. Esta é a propriedade da chave

[Column(Storage="_ParentKey", DbType="Int")]
public System.Nullable<int> ParentKey
{
    get
    {
        return this._ParentKey;
    }
    set
    {
        if ((this._ParentKey != value))
        {
            //This code is added by the association
            if (this._Parent.HasLoadedOrAssignedValue)
            {
                throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
            }
            //This code is present regardless of association
            this.OnParentKeyChanging(value);
            this.SendPropertyChanging();
            this._ParentKey = value;
            this.SendPropertyChanged("ParentKey");
            this.OnServiceAddrIDChanged();
        }
    }
}

E esta é a propriedade associações.

[Association(Name="Parent_Child", Storage="_Parent", ThisKey="ParentKey", IsForeignKey=true, DeleteRule="CASCADE")]
public Parent Parent
{
    get
    {
        return this._Parent.Entity;
    }
    set
    {
        Parent previousValue = this._Parent.Entity;
        if (((previousValue != value) 
                    || (this._Parent.HasLoadedOrAssignedValue == false)))
        {
            this.SendPropertyChanging();
            if ((previousValue != null))
            {
                this._Parent.Entity = null;
                previousValue.Exemptions.Remove(this);
            }
            this._Parent.Entity = value;
            if ((value != null))
            {
                value.Exemptions.Add(this);
                this._ParentKey = value.ParentKey;
            }
            else
            {
                this._ParentKey = default(Nullable<int>);
            }
            this.SendPropertyChanged("Parent");
        }
    }
}

É melhor para mudanças atribuir através da associação em vez da chave. Dessa forma, você não tem que se preocupar se o pai é carregado.

Outras dicas

Table1:       Table2:
ID            ID
Name          Description
              ForeignID

Com este:

Table2.ForeignID = 2

você receber um erro ..........

Exemplo:

Você pode alterar o campo ForeignID na Tabela 2 Whit esta:

   Table2 table = dataContext.Table2.single(d => d.ID == Id)

   table.Table1 = dataContext.Table1.single(d => d.ID == newId);

Quando o newId variável é o id do registro na Tabela 2 que você gostaria associado Whit o registro na Tabela 1

Você quer se associar com um outro registro na tabela 1 ou mudança table1.id? Se é a opção 1, você precisa remover essa associação e definir um novo. Se a opção 2, verificar se você DB e ver se sim atualização em cascata habilitado para este fk e que o recorde get e altere o valor da id.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top