ERROR: An entity can only be attached as modified...

onmyway

Member
Joined
Jan 18, 2016
Messages
12
Programming Experience
Beginner
Hi all,

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.

My setup:

I am using VS2015 Community Edition
I am using LINQ to SQL
.Net 4.5

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:

C#:
[COLOR=black]sourceContext[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]ProjectMasters[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Attach[/COLOR][COLOR=black]([/COLOR][COLOR=black]project[/COLOR][COLOR=black],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=black]);[/COLOR]

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.


C#:
[COLOR=#00008B]private[/COLOR][COLOR=#00008B]class[/COLOR][COLOR=#2B91AF]ProjectMastersIdentifier[/COLOR][COLOR=black]
            [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                [/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#2B91AF]int[/COLOR][COLOR=#2B91AF]Id[/COLOR][COLOR=black]{[/COLOR][COLOR=black] get[/COLOR][COLOR=black];[/COLOR][COLOR=#2B91AF]set[/COLOR][COLOR=black];[/COLOR][COLOR=black]}[/COLOR][COLOR=black]
                [/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]string[/COLOR][COLOR=#2B91AF]FinanceProjectNumber[/COLOR][COLOR=black]{[/COLOR][COLOR=black] get[/COLOR][COLOR=black];[/COLOR][COLOR=#2B91AF]set[/COLOR][COLOR=black];[/COLOR][COLOR=black]}[/COLOR][COLOR=black]
                [/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]string[/COLOR][COLOR=#2B91AF]ProcessingSignal[/COLOR][COLOR=black]{[/COLOR][COLOR=black] get[/COLOR][COLOR=black];[/COLOR][COLOR=#2B91AF]set[/COLOR][COLOR=black];[/COLOR][COLOR=black]}[/COLOR][COLOR=black]
            [/COLOR][COLOR=black]}[/COLOR]

C#:
[COLOR=#00008B]public[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=#00008B]void[/COLOR][COLOR=#2B91AF]ProcessInstructions[/COLOR][COLOR=black]()[/COLOR][COLOR=black]
        [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
            using [/COLOR][COLOR=black]([/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] sourceContext [/COLOR][COLOR=black]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]StagingTableDataContext[/COLOR][COLOR=black]())[/COLOR][COLOR=black]
            [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                using [/COLOR][COLOR=black]([/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] destinationContext [/COLOR][COLOR=black]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]DestinationTableDataContext[/COLOR][COLOR=black]())[/COLOR][COLOR=black]
                [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                    [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] allProjectNames [/COLOR][COLOR=black]=[/COLOR][COLOR=black] destinationContext[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]THEOPTIONs[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Select[/COLOR][COLOR=black]([/COLOR][COLOR=black]u [/COLOR][COLOR=black]=>[/COLOR][COLOR=black] u[/COLOR][COLOR=black].[/COLOR][COLOR=black]NAME[/COLOR][COLOR=black]).[/COLOR][COLOR=#2B91AF]Distinct[/COLOR][COLOR=black]().[/COLOR][COLOR=#2B91AF]ToList[/COLOR][COLOR=black]();[/COLOR][COLOR=black]

                    [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] instructionGroups [/COLOR][COLOR=black]=[/COLOR][COLOR=black] sourceContext[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]ProjectMasters[/COLOR][COLOR=black].[/COLOR][COLOR=black]
                        [/COLOR][COLOR=#2B91AF]Where[/COLOR][COLOR=black]([/COLOR][COLOR=black]u [/COLOR][COLOR=black]=>[/COLOR][COLOR=black] u[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Processing_Result[/COLOR][COLOR=black]==[/COLOR][COLOR=#2B91AF]Unprocessed[/COLOR][COLOR=black]).[/COLOR][COLOR=black]
                        [/COLOR][COLOR=#2B91AF]Select[/COLOR][COLOR=black]([/COLOR][COLOR=black]
                        u [/COLOR][COLOR=black]=>[/COLOR][COLOR=black]
                            [/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]ProjectMastersIdentifier[/COLOR][COLOR=black]
                            [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                                [/COLOR][COLOR=#2B91AF]Id[/COLOR][COLOR=black]=[/COLOR][COLOR=black] u[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]RowID[/COLOR][COLOR=black],[/COLOR][COLOR=black]
                                [/COLOR][COLOR=#2B91AF]FinanceProjectNumber[/COLOR][COLOR=black]=[/COLOR][COLOR=black] u[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Finance_Project_Number[/COLOR][COLOR=black],[/COLOR][COLOR=black]
                                [/COLOR][COLOR=#2B91AF]ProcessingSignal[/COLOR][COLOR=black]=[/COLOR][COLOR=black] u[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Processing_Signal[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Trim[/COLOR][COLOR=black]()[/COLOR][COLOR=black]
                            [/COLOR][COLOR=black]}[/COLOR][COLOR=black]
                        [/COLOR][COLOR=black]).[/COLOR][COLOR=#2B91AF]GroupBy[/COLOR][COLOR=black]([/COLOR][COLOR=black]u [/COLOR][COLOR=black]=>[/COLOR][COLOR=black] u[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]ProcessingSignal[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Trim[/COLOR][COLOR=black]());[/COLOR][COLOR=black]

                    [/COLOR][COLOR=#00008B]foreach[/COLOR][COLOR=black]([/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] instructionGroup [/COLOR][COLOR=#00008B]in[/COLOR][COLOR=black] instructionGroups[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Where[/COLOR][COLOR=black]([/COLOR][COLOR=black]u [/COLOR][COLOR=black]=>[/COLOR][COLOR=black] u[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Key[/COLOR][COLOR=black]==[/COLOR][COLOR=maroon]"I"[/COLOR][COLOR=black]))[/COLOR][COLOR=black]
                    [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                        [/COLOR][COLOR=#00008B]foreach[/COLOR][COLOR=black]([/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] instruction [/COLOR][COLOR=#00008B]in[/COLOR][COLOR=black] instructionGroup[/COLOR][COLOR=black])[/COLOR][COLOR=black]
                        [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                            [/COLOR][COLOR=#00008B]if[/COLOR][COLOR=black]([/COLOR][COLOR=black]allProjectNames[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Contains[/COLOR][COLOR=black]([/COLOR][COLOR=black]instruction[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]FinanceProjectNumber[/COLOR][COLOR=black]))[/COLOR][COLOR=black]
                            [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                                [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] project [/COLOR][COLOR=black]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]ProjectMaster[/COLOR][COLOR=black]
                                [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                                    [/COLOR][COLOR=#2B91AF]RowID[/COLOR][COLOR=black]=[/COLOR][COLOR=black] instruction[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Id[/COLOR][COLOR=black],[/COLOR][COLOR=black]
                                    [/COLOR][COLOR=#2B91AF]Processing_Result[/COLOR][COLOR=black]=[/COLOR][COLOR=#2B91AF]Invalid[/COLOR][COLOR=black],[/COLOR][COLOR=black]
                                    [/COLOR][COLOR=#2B91AF]Processing_Result_Text[/COLOR][COLOR=black]=[/COLOR][COLOR=maroon]"Project Already Exists"[/COLOR][COLOR=black]
                                [/COLOR][COLOR=black]};[/COLOR][COLOR=black]
                                sourceContext[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]ProjectMasters[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Attach[/COLOR][COLOR=black]([/COLOR][COLOR=black]project[/COLOR][COLOR=black],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=black]);[/COLOR][COLOR=black]
                            [/COLOR][COLOR=black]}[/COLOR][COLOR=black]
                            [/COLOR][COLOR=#00008B]else[/COLOR][COLOR=black]
                            [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                                [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=black] project [/COLOR][COLOR=black]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]ProjectMaster[/COLOR][COLOR=black]
                                [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
                                    [/COLOR][COLOR=#2B91AF]RowID[/COLOR][COLOR=black]=[/COLOR][COLOR=black] instruction[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Id[/COLOR][COLOR=black],[/COLOR][COLOR=black]
                                    [/COLOR][COLOR=#2B91AF]Processing_Result[/COLOR][COLOR=black]=[/COLOR][COLOR=#2B91AF]Unprocessed[/COLOR][COLOR=black],[/COLOR][COLOR=black]
                                    [/COLOR][COLOR=#2B91AF]Processing_Result_Text[/COLOR][COLOR=black]=[/COLOR][COLOR=maroon]"Project Does not Exiist...Moving on."[/COLOR][COLOR=black]
                                [/COLOR][COLOR=black]};[/COLOR][COLOR=black]
                                sourceContext[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]ProjectMasters[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]Attach[/COLOR][COLOR=black]([/COLOR][COLOR=black]project[/COLOR][COLOR=black],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=black]);[/COLOR][COLOR=black]
                            [/COLOR][COLOR=black]}[/COLOR][COLOR=black]
                        [/COLOR][COLOR=black]}[/COLOR][COLOR=black]

                sourceContext[/COLOR][COLOR=black].[/COLOR][COLOR=#2B91AF]SubmitChanges[/COLOR][COLOR=black]();[/COLOR][COLOR=black]
            [/COLOR][COLOR=black]}[/COLOR][COLOR=black]
        [/COLOR][COLOR=black]}[/COLOR][COLOR=black]
    [/COLOR][COLOR=black]}

[/COLOR]

Thank you in advance!
 
Top Bottom