C# 如何更改关联字段的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/197753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:39:38  来源:igfitidea点击:

How to change the value of associated field

c#linqlinq-to-sqlassociations

提问by HAdes

I have 2 classes with a LINQ association between them i.e.:

我有 2 个类,它们之间有 LINQ 关联,即:

Table1:       Table2:
ID            ID
Name          Description
              ForiegnID

The association here is between Table1.ID -> Table2.ForiegnID

这里的关联是Table1.ID -> Table2.ForiegnID

I need to be able to change the value of Table2.ForiegnID, however I can't and think it is because of the association (as when I remove it, it works).

我需要能够更改 Table2.ForiegnID 的值,但是我不能并且认为这是因为关联(因为当我删除它时,它可以工作)。

Therefore, does anyone know how I can change the value of the associated field Table2.ForiegnID?

因此,有谁知道如何更改关联字段 Table2.ForiegnID 的值?

采纳答案by Amy B

Check out the designer.cs file. This is the key's property

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

And this is the associations property.

这是协会的财产。

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

It's best to assign changes through the association instead of the key. That way, you don't have to worry about whether the parent is loaded.

最好通过关联而不是键来分配更改。这样,您就不必担心父级是否已加载。

回答by Zote

You wanna to associate with another record in table1 or change table1.id? if it's option 1, you need to remove that association and set a new one. If option 2, check you db and see if update cascade yes enabled for this fk and than get record and change value of id.

您想与 table1 中的另一条记录关联或更改 table1.id?如果是选项 1,则需要删除该关联并设置一个新关联。如果选项 2,请检查您的数据库并查看是否为此 fk 启用了更新级联是,然后获取记录并更改 id 的值。

回答by Amy B

Table1:       Table2:
ID            ID
Name          Description
              ForeignID

With this :

有了这个 :

Table2.ForeignID = 2

Table2.ForeignID = 2

you receive an error..........

你收到一个错误......

Example :

例子 :

You can change ForeignID field in Table 2 whit this :

您可以更改表 2 中的 ForeignID 字段:

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

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

Where the variable newIdis the id of the record in Table 2 that would you like associate whit the record in Table 1

其中变量newId是表 2 中记录的 id,您希望将其与表 1 中的记录相关联