0

我得到了以下具有有效关系的表格,如下所示:

 Report
      ------>ReprotDataSource
                --------->SharePointDomain

现在,当我尝试以下操作(将新的 ReprotDataSource 链接到选定的 SharePointDomain)时,它会插入一个新的 SharePointDomain 记录,而不是将其引用到 ID 为 (2) 的 SharePointDomain

//Create new Object
ReportDataSource rprtDS = new ReportDataSource
{
  Name = rprtDSSelected.Name,
  Parent = rprtDSSelected.Parent,
  CreatedBy = Environment.UserName,
  CreationDate = DateTime.Now,
  Source = rprtDSSelected.Source,
  Type = rprtDSSelected.Type
};

  if (rprtDS.Type == "SP List")
  //here is the issue
  rprtDS.SharePointDomain = selectedSharePointDomain;//its id = 2
  //Add to EntitySet
  TheReport.ReportDataSources.Add(rprtDS);
  TheReport.Save();

当我将自己的 id 设置为 (2) 时,它工作正常

任何解释。?

先感谢您。

4

1 回答 1

1

您添加的对象必须来自相同的数据上下文,否则它将被视为隐式插入。我猜这个物体来自其他地方;也许是以前的数据上下文。如果您在查询之间缓存对象,这很棘手。也许只是设置 id 代替...:p

您可能会在必要时获得一些分离和依附的乐趣,但这可能不值得。

于 2010-12-05T13:50:37.977 回答