我有2个教学班,即它们之间的关联LINQ。
I have 2 classes with a LINQ association between them i.e.:
Table1: Table2: ID ID Name Description ForiegnID这里的关联 Table1.ID之间 - > Table2.ForiegnID
我需要能够改变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?
推荐答案退房designer.cs文件。这是关键的属性。
Check out the designer.cs file. This is the key's property
[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"); } } }这是最好的分配通过协会,而不是关键的变化。这样,你就不必担心父母是否已加载。
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.
更多推荐
如何改变相关联的字段的值
发布评论