6

私はそれらの間にLINQの関連付けを持つ2つのクラスを持っています。

Table1:       Table2:
ID            ID
Name          Description
              ForiegnID

ここでの関連付けは、Table1.ID -> Table2.ForiegnIDの間です。

Table2.ForiegnID の値を変更できるようにする必要がありますが、関連付けが原因であるとは思えません (削除すると機能するため)。

したがって、関連するフィールド Table2.ForiegnID の値を変更する方法を知っている人はいますか?

4

3 に答える 3

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

そしてこれはassociationsプロパティです。

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

キーではなく、関連付けを介して変更を割り当てることをお勧めします。そうすれば、親がロードされているかどうかを心配する必要はありません。

于 2008-10-13T14:44:52.393 に答える
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は、表 1 のレコードと関連付ける表 2 のレコードの ID です。

于 2009-05-08T12:56:48.557 に答える
0

table1の別のレコードに関連付けるか、table1.idを変更しますか?オプション1の場合は、その関連付けを削除して、新しい関連付けを設定する必要があります。オプション2の場合、dbをチェックし、このfkに対して更新カスケードyesが有効になっているかどうかを確認し、レコードを取得してidの値を変更します。

于 2008-10-13T14:41:13.990 に答える