SubmitChanges Inserts new record for foreign relation instead of use the assigned one
I got the following Tables with valid relations as shown below: 开发者_StackOverflow社区
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.
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.
精彩评论