SubmitChanges 为外部关系插入新记录而不是使用指定的记录
我得到了以下具有有效关系的表,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要添加的对象必须来自相同的数据上下文,否则它将被视为隐式插入。我猜这个物体来自其他地方;也许是以前的数据上下文。如果您在查询之间缓存对象,这会很棘手。也许只是设置 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.