문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top