Fluent NHibernate:无法将 PersistentGenericBag 转换为类型“System.Collections.Generic.ISet”
我有一个对象主题,它是一个自相关的层次结构,其中子主题是在
public class Topic : Entity {
public ISet<Topic> ChildTopics { get; internal set; }
public Topic ParentTopic { get; set; }
...
}
我编写表单(MVC3)时定义的,以生成第一级主题的下拉列表(Html.DropDownListFor)(理论上这将最终 AJAX 进入第二级主题的第二个下拉菜单),但是当它进行保存时,它会产生非常流行的“无法投射...”异常(参见问题标题)。
造成这种情况的通常原因是您使用了 List 或 Set 而不是 IList 或 ISet,但我使用的是 ISet,并且它明确表示它无法转换为 ISet。
这是一个集合的原因是因为您不希望一个主题多次成为另一个主题的子主题。通过以下覆盖,Fluent NH automapping 创建的表映射是正确的:
mapping.HasMany
I have an object Topic which is a self-related hierarchy where the child Topics are defined as
public class Topic : Entity {
public ISet<Topic> ChildTopics { get; internal set; }
public Topic ParentTopic { get; set; }
...
}
I'm writing a form (MVC3) to produce a drop-down list (Html.DropDownListFor) of the first-level topics (theoretically this will eventually AJAX into a second drop-down for second-level topics), but when it goes to save, it produces the ever-popular "Cannot cast..." exception (see question title).
The usual cause of this is that you used List or Set instead of IList or ISet, but I am using ISet, and it specifically says it can't cast to ISet.
The reason this is a set is because you wouldn't want a Topic being a child of another Topic more than once. The table mapping create by Fluent NH automapping is correct with this override:
mapping.HasMany<Topic>(t => t.ChildTopics).AsSet().Inverse().KeyColumn("TopicId");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的项目中,从 NHibernate 3.2.0.400 开始,如果我使用 System.Collections.Generic.ISet而不是 Iesi.Collections.Generic.ISet,仍然会发生此错误。 。只需更改周围的引用即可完成最少的工作并解决问题。
In my project, as of NHibernate 3.2.0.400, this error still occurs if I use
System.Collections.Generic.ISet<T>
instead ofIesi.Collections.Generic.ISet<T>
. Simply changing the references around is minimal work and solves the problem.