Entity framework and inheritance: NotSupportedException

前端 未结 3 1274
暖寄归人
暖寄归人 2021-02-20 16:02

I\'m getting

System.NotSupportedException: All objects in the EntitySet \'Entities.Message\' must have unique primary keys. However, an instance of

相关标签:
3条回答
  • 2021-02-20 16:37

    I am not an EF kind of guy (busy working with NHibernate, haven't had time to get up to date with EF yet) so I may be totally wrong here, but could the problem be that the two tables (since you are using inheritance by table-per-type) have primary keys that collide?

    If you check the data in both tables, do primary key values collide?

    0 讨论(0)
  • 2021-02-20 16:43

    It sounds like you are pulling two records into memory one into message and one into comment.

    Possible prblems:

    • There are two physical messages with the same id
    • The same message is being pulled up as a message and a comment
    • The same message is being pulled up twice into the same context

    That the problem sometimes goes away when you restart, points to a problem with cleaning up of context. Are you using "using" statements.

    Do you have functionality for changing from a message to a comment?

    0 讨论(0)
  • 2021-02-20 16:44

    As you infer, it looks like the Context is fetching a Comment as a Message (not knowing that it is a comment). Later, you ask for the actual Comment, so the context fetches the Comment. Now you have two object instances in the Context with the same ID - one is a Message and one is a Comment.

    It seems that the exception is not being thrown until after both objects have been loaded (ie when you try to access the Message the second time). If you can find a way to remove the Message from Context when the Comment is loaded, this may solve your problem.

    Another option might be to use the Table-per-hierarchy model. This results in a bad database design but at the end of the day you have to use what works.

    You might be able to avoid the problem by ensuring that the objects are loaded as Comments first. This way, when you ask for the Message, the Context already knows about it.

    Also consider using Composition over Inheritance, such that a Message has 0..1 CommentDetails.

    The final suggestion is to remove the dependency on the Entity Framework from your Control code, and create a Data Access Layer which references the EF and retrieves your objects. The DAL can turn Entity Framework objects into a different set of Entity objects which are easier to use in code. This approach will produce a lot of code overhead, but may be suitable if you cannot use the Entity Framework to produce an Entity model which represents your Entities in the way you want to work with them.

    To summarize, unless MS fix this issue, there is no solution to your problem which does not involve a rethink of your approach. Unfortunately the Entity Framework is not ideal, especially for complex Entity models - you might be better off creating your own DAL and bypassing the EF altogether.

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