2008-10-13 17 views
6

tengo 2 clases con una asociación entre ellos LINQ es decir:Cómo cambiar el valor del campo asociado

Table1:  Table2: 
ID   ID 
Name   Description 
       ForiegnID 

La asociación aquí es entre Table1.ID -> Table2.ForiegnID

I necesita poder cambiar el valor de Table2.ForiegnID, sin embargo no puedo y creo que es debido a la asociación (como cuando lo elimino, funciona).

Por lo tanto, ¿alguien sabe cómo puedo cambiar el valor del campo asociado Table2.ForiegnID?

Respuesta

7

Echa un vistazo al archivo designer.cs. Esta es la propiedad de la clave

[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(); 
     } 
    } 
} 

Y esta es la propiedad de asociaciones.

[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"); 
     } 
    } 
} 

Lo mejor es asignar los cambios mediante la asociación en lugar de la clave. De esta forma, no tiene que preocuparse de si el padre está cargado.

+0

Podría ser a algo allí, por favor ¿Puede ampliar que – HAdes

+0

Gracias que tiene más sentido. – HAdes

0

¿Desea asociar con otro registro en table1 o cambiar table1.id? si es la opción 1, debe eliminar esa asociación y establecer una nueva. Si es la opción 2, verifique su DB y vea si la cascada de actualización sí está habilitada para este fk y luego obtenga el registro y cambie el valor de id.

1
Table1:  Table2: 
ID   ID 
Name   Description 
       ForeignID 

Con esta:

Table2.ForeignID = 2

recibe un error ..........

Ejemplo:

Usted puede cambiar Campo ForeignID en la Tabla 2 con esta:

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

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

donde la variable newId es el ID del registro en la tabla 2 que le gustaría tener asociado ápice el registro en la Tabla 1

Cuestiones relacionadas