错误:实体只能以修改后的形式附加...

我在途中

成员
已加入
2016年1月18日
留言内容
12
编程经验
Beginner
大家好,

我希望你能帮助我。这是我几天来一直在努力解决的问题,我正在努力解决。

我的设置:

我正在使用VS2015社区版
我正在使用LINQ to SQL
.Net 4.5

我已经阅读了很多有关此特定错误的信息,并且似乎有几种解决方法。不幸的是我没有能够。

我的错误在于此行:

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]

我收到错误消息:如果实体声明了版本成员或没有更新检查策略,则只能以修改后的形式附加而没有原始状态。

我已阅读并尝试(如果可能)以下解决方案:

LINQ中的实体附件问题
使用DataContext Attach方法更新实体时的“更新检查”问题
System.InvalidOperationException:只能按修改的方式附加实体

和更多 ...

似乎问题就在于此;我不会更新未更改的记录。但是,据我所见和了解,记录已更改。
我敢肯定,这是我做错的简单事情,也许只是需要一些新的眼光。

请记住;我是新手 :) 感谢您的耐心等待。


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]

先感谢您!
 
最佳 底部