实体框架和继承:NotSupportedException [英] Entity framework and inheritance: NotSupportedException

查看:104
本文介绍了实体框架和继承:NotSupportedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获得


System.NotSupportedException:EntitySet
'Entities.Message中的所有
对象'必须有唯一的
主键。但是,
类型为Model.Message的实例和Model.Comment类型的实例
都具有
相同的主键值

System.NotSupportedException: All objects in the EntitySet 'Entities.Message' must have unique primary keys. However, an instance of type 'Model.Message' and an instance of type 'Model.Comment' both have the same primary key value

但我不知道这是什么意思。

but I have no idea what this means.

使用EF4,我有一堆类型的实体。这些消息中的一些实际上是一个子类型,注释,按表类型继承。只是

Using EF4, I have a bunch of entities of type Message. Some of these messages are actually a subtype, Comment, inheritance by table-per-type. Just

 DB.Message.First();

将产生异常。我有其他子类型的实例,我没有遇到问题,但我看不到任何差异。有时候,如果我重新启动开发服务器,但是并不总是这个问题就消失了。

will produce the exception. I have other instances of subtyping where I don't experience problems but I can't see any discrepencies. Sometimes, though, the problem goes away if I restart the development server, but not always.

编辑:
我已经制定出来了(应该有)该问题是存储过程获取我的消息的错误。当前所设置的方式是与Message相关的所有字段都被提取,则注释表被sproc忽略。然后,上下文进行处理,可能通过再次提取那些也是评论的消息,如您所建议的那样。如何妥善处理是中心问题。我在 http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bb0bb421-ba8e-4b35-b7a7-950901adb602

推荐答案

如你所推断,它看起来像是将注释作为消息获取(不知道它是一个注释)。后来你要求实际的评论,所以上下文提取评论。现在你有两个对象实例在上下文中具有相同的ID - 一个是一个消息,一个是一个注释。

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.

还要考虑使用构成继承,使消息有0..1 CommentDetails。

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

最终建议是从控制代码中删除对实体框架的依赖,并创建一个引用EF并检索对象的数据访问层。 DAL可以将Entity Framework对象转换为在代码中更容易使用的不同的Entity对象集合。这种方法会产生大量的代码开销,但是如果您不能使用实体框架来生成实体模型,那么您可以按照您想要处理这些实体的方式来表示您的实体。

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.

总而言之,除非MS解决这个问题,否则您的问题无法解决,不涉及对您的方法的反思。不幸的是,实体框架不是理想的,特别是对于复杂的实体模型,您可能最好创建自己的DAL并绕过EF。

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.

这篇关于实体框架和继承:NotSupportedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆