开发者

Logically Why Can't Change the ParentID for an Object, instead I must change the parent it self?

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

Why not can I set the Parent ID directly? what may let LINQ Prevent such an action

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

Exception Thrown here

[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();
      开发者_如何学JAVA      }
            this.OnParentIDChanging(value);
            this.SendPropertyChanging();
            this._ParentID = value;
            this.SendPropertyChanged("ParentID");
            this.OnParentIDChanged();
        }
    }
}


There is an unfortunate duplicity between ParentId and Parent; having them disagree is a pain, so it doesn't let you do that. You can set just the id though - especially when inserting.

You could try:

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

Then they can't be in conflict.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜