2008-10-13 2 views
6

관련된 필드의 값을 변경하는 방법 예 :내가 그들 사이에 LINQ 협회와 함께이 개 수업을

Table1:  Table2: 
ID   ID 
Name   Description 
       ForiegnID 

여기 협회는 Table1.ID 사이에 -> Table2.ForiegnID

I 그러나 Table2.ForiegnID의 값을 변경할 수 있어야합니다. 그러나 연관성 때문이라고 생각하지 않습니다 (제거 할 때와 같이 작동합니다).

따라서 누구나 관련 필드 Table2.ForiegnID의 값을 어떻게 변경할 수 있는지 알고 있습니까?

답변

7

designer.cs 파일을 확인하십시오. 이것은 키의 속성입니다.

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

그리고 이것은 연관 속성입니다.

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

키 대신 연결을 통해 변경 사항을 할당하는 것이 가장 좋습니다. 그렇게하면 부모가로드되었는지 여부에 대해 걱정할 필요가 없습니다. 이와

+0

이 될 수 있습니다. – HAdes

+0

감사합니다. 감사합니다. – HAdes

0

table1의 다른 레코드와 연결하거나 table1.id를 변경 하시겠습니까? 옵션 1 인 경우 해당 연결을 제거하고 새 연결을 설정해야합니다. 옵션 2 인 경우, db를 확인하고이 fk에 대해 update cascade가 활성화되어 있는지 확인하고 레코드를 얻고 id 값을 변경하십시오.

1
Table1:  Table2: 
ID   ID 
Name   Description 
       ForeignID 

:

Table2.ForeignID = 2

당신이 오류가 발생 ..........

예 :

당신은 변경할 수 있습니다 표 2의 ForeignID 필드는 다음과 같습니다.

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

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

여기서 변수 newId은 표 2의 레코드 ID입니다. 표 1의 레코드를 연관 시키려면

관련 문제