2010-11-28 19 views
2
//Set Parent ID for the rest of the Reports data sources 
this.ReportDataSources.ToList().ForEach(rds => rds.Parent = reportDataSource); 

为什么我不能直接设置父ID?什么可以让LINQ防止这样的动作逻辑上,为什么不能改变对象的ParentID,而是我必须改变父母它自己?

//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(); 
     } 
    } 
} 
+0

当然,我调试它,我发布在代码上的异常:抛出新的System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();每当ParentId改变时抛出。 – 2010-11-28 08:49:10

回答

2

还有的ParentId和家长之间的不幸口是心非;让他们不同意是一种痛苦,所以它不会让你这样做。你可以设置只是编号虽然 - 尤其是当插入。

你可以尝试:

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

然后,他们不能有冲突。

+0

那么这个属性有什么意义:this._Parent.HasLoadedOrAssignedValue,因为我可以解决它? – 2010-11-28 09:01:00

相关问题