加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何更改关联字段的值

发布时间:2020-12-15 08:25:37 所属栏目:百科 来源:网络整理
导读:我有2个类,它们之间有LINQ关联,即: Table1: Table2:ID IDName Description ForiegnID 这里的关联是在Table1.ID – 之间. Table2.ForiegnID 我需要能够更改Table2.ForiegnID的值,但我不能并且认为这是因为关联(因为当我删除它时,它工作). 因此,有谁知道如何
我有2个类,它们之间有LINQ关联,即:
Table1:       Table2:
ID            ID
Name          Description
              ForiegnID

这里的关联是在Table1.ID – >之间. Table2.ForiegnID

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

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

解决方法

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

最好通过关联而不是密钥来分配更改.这样,您不必担心父项是否已加载.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读