How to change the value of associated field

后端 未结 3 1535
死守一世寂寞
死守一世寂寞 2021-01-05 15:59

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

Table1:       Table2:
ID            ID
Name          Description
              ForiegnID
<         


        
3条回答
  •  渐次进展
    2021-01-05 17:01

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

    [Column(Storage="_ParentKey", DbType="Int")]
    public System.Nullable 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);
                }
                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.

提交回复
热议问题