onmyway365 .

onmyway365 .

  • NA
  • 39
  • 4.6k

Error in C# Procedure that compares 2 tables

Jan 23 2016 11:00 AM

Hi guys,

I Hope you can help me.
 
I am trying to create a procedure that compares two DB tables, find the DISTINCT records, and update them.  
 
However, I am getting an error in my procedure on this line:
 
 stageContext.ProjectMasters.Attach(project, true);
 

An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.

 
Here is my procedure: 

private class ProjectMastersIdentifier
{
public int Id { get; set; }
public string FinanceProjectNumber { get; set; }
}
public static void NewRecords()
{
using (var stageContext = new StagingTableDataContext())
{
using (var destinationContext = new DestinationTableDataContext())
{
var allProjectNames = destinationContext.THEOPTIONs.Select(u => u.NAME).Distinct().ToList();
var projectMasters = stageContext.ProjectMasters.Select(
u =>
new ProjectMastersIdentifier
{
Id = u.RowID,
FinanceProjectNumber = u.Finance_Project_Number
}
).Distinct();
foreach (var projectIdentifier in projectMasters)
{
if (!allProjectNames.Contains(projectIdentifier.FinanceProjectNumber))
{
var project = new ProjectMaster
{
RowID = projectIdentifier.Id,
Processing_Result = 0,
Processing_Result_Text = "UNIQUE"
};
stageContext.ProjectMasters.Attach(project, true);
}
}
}
stageContext.SubmitChanges();
}
}
 
Thank you in advance! 

Answers (3)