[英]SubmitChanges not updating, but inserts new record. LINQ to SQL
[英]SubmitChanges Inserts new record for foreign relation instead of use the assigned one
我得到了具有有效关系的下表,如下所示:
Report
------>ReprotDataSource
--------->SharePointDomain
现在,当我尝试以下操作(将新的ReprotDataSource链接到Selected 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)时,效果很好
有什么解释吗?
先感谢您。
您要添加的对象必须来自相同的数据上下文,否则它将被视为隐式插入。 我猜这个对象来自其他地方。 以前的数据环境。 如果要在查询之间缓存对象,这将很棘手。 也许只是设置id代替...:p
您可能会在需要时进行一些分离和附加的操作,但这可能不值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.