如何改变相关联的字段的值

编程入门 行业动态 更新时间:2024-10-24 20:19:04
本文介绍了如何改变相关联的字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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.

更多推荐

如何改变相关联的字段的值

本文发布于:2023-10-19 14:45:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:相关联   字段

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!