SubmitChanges 为外部关系插入新记录而不是使用指定的记录

发布于 2024-10-05 18:23:54 字数 833 浏览 3 评论 0原文

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

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

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

//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) 时

有什么解释吗?

先感谢您。

I got the following Tables with valid relations as shown below:

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

Now, when i try the following ( link the newly ReprotDataSource to the Selected SharePointDomain) it insertes a new SharePointDomain Record instead of refrence it to the SharePointDomain with id (2)

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

It works fine when i set the id my self to (2)

any explanations.?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-10-12 18:23:55

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

您可能会因为必要时分离和连接而感到高兴,但这可能不值得。

The object you are adding must come from the same data-context, otherwise it will count as an implicit insert. I'm guessing this object has come from elsewhere; a previous data-context perhaps. This is tricky if you are caching the object between queries. Maybe just set the id instead... :p

You might have some joy detaching and attaching as necessary, but it probably isn't worth it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文