I hope you can help me. This is an issue I have been battling with for days now, and I am struggling to resolve it.
I have read up quite a bit on this specific error, and it seems there are a couple ways to resolve it. I have unfortunately not been able to.
My error lies on this line:
sourceContext.ProjectMasters.Attach(project, true);
I get the error: 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.
I have read and tried (where possible) the following solutions:
Entity attachment issues in LINQ
Update check' issue when updating an entity using DataContext Attach method
System.InvalidOperationException: An entity can only be attached as modified
And more ...
It seems the problem lies in this; that I am not updating an unchanged record. However, as far as I can see and understand, the record IS changed.
I am sure it is something simple I am doing wrong, and perhaps just need a set of new eyes on it.
Please remember; I am a newbie :) Thank you for your patience.
private class ProjectMastersIdentifier
{
public int Id { get; set; }
public string FinanceProjectNumber { get; set; }
public string ProcessingSignal { get; set; }
}
public static void ProcessInstructions()
{
using (var sourceContext = new StagingTableDataContext())
{
using (var destinationContext = new DestinationTableDataContext())
{
var allProjectNames = destinationContext.THEOPTIONs.Select(u => u.NAME).Distinct().ToList();
var instructionGroups = sourceContext.ProjectMasters.
Where(u => u.Processing_Result == Unprocessed).
Select(
u =>
new ProjectMastersIdentifier
{
Id = u.RowID,
FinanceProjectNumber = u.Finance_Project_Number,
ProcessingSignal = u.Processing_Signal.Trim()
}
).GroupBy(u => u.ProcessingSignal.Trim());
foreach (var instructionGroup in instructionGroups.Where(u => u.Key == "I"))
{
foreach (var instruction in instructionGroup)
{
if (allProjectNames.Contains(instruction.FinanceProjectNumber))
{
var project = new ProjectMaster
{
RowID = instruction.Id,
Processing_Result = Invalid,
Processing_Result_Text = "Project Already Exists"
};
sourceContext.ProjectMasters.Attach(project, true);
}
else
{
var project = new ProjectMaster
{
RowID = instruction.Id,
Processing_Result = Unprocessed,
Processing_Result_Text = "Project Does not Exiist...Moving on."
};
sourceContext.ProjectMasters.Attach(project, true);
}
}
sourceContext.SubmitChanges();
}
}
}
Thank you in advance!