Explicit construction of entity type [MyClass] in query is not allowed

前端 未结 2 1414
盖世英雄少女心
盖世英雄少女心 2020-12-03 15:54

Like the title says, I have the following exception:

Description: Event code: 3005 Event message: An unhandled exception has occurred. Excepti

相关标签:
2条回答
  • 2020-12-03 16:24

    Entities that are part of the data context can not be created using a LINQ query. This is a well thought design decision of the C# team. Because the entities are newed up (manually) in the Select statement, this would mean that they are not tracked by the DataContext and this can confuse developers. On the other hand, when the DataContext would automatically insert on submit those newed up entities, this would be confusing as well. The only option left was communicating to the developers that this is not such a good idea to do, and that is what you saw happening.

    0 讨论(0)
  • 2020-12-03 16:27

    It will work if you don't have PrimaryKey set on any columns in your Friends class. That way changes won't be tracked for that class anymore, but your code will work.

    0 讨论(0)
提交回复
热议问题