gpt4 book ai didi

c# - 从逻辑上讲,为什么不能更改对象的 ParentID,而我必须自己更改父对象?

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:47 28 4
gpt4 key购买 nike

//Set Parent ID for the rest of the Reports data sources
this.ReportDataSources.ToList().ForEach(rds => rds.Parent = reportDataSource);

为什么不能直接设置Parent ID?什么可以让 LINQ 阻止这样的操作

//Set Parent ID for the rest of the Reports data sources
this.ReportDataSources.ToList().ForEach(rds => rds.ParentID = reportDataSource.ID);

此处抛出异常

[Column(Storage="_ParentID", DbType="Int")]
public System.Nullable<int> ParentID
{
get
{
return this._ParentID;
}
set
{
if ((this._ParentID != value))
{
if (this._Parent.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnParentIDChanging(value);
this.SendPropertyChanging();
this._ParentID = value;
this.SendPropertyChanged("ParentID");
this.OnParentIDChanged();
}
}
}

最佳答案

不幸的是,ParentId 和 Parent 之间存在口是心非;让他们不同意是一种痛苦,所以它不会让你那样做。不过,您可以只设置 id - 尤其是在插入时。

你可以试试:

obj.Parent = null;
obj.ParentId = newParentId;

那么他们就不会发生冲突。

关于c# - 从逻辑上讲,为什么不能更改对象的 ParentID,而我必须自己更改父对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4296019/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com