NHibernate代理引起数据绑定问题 [英] NHibernate proxy causing problems with databinding
问题描述
System.Reflection.TargetException:对象不匹配目标类型
似乎问题是由于数据绑定无法处理列表中的第一个项目,而是其他项目的子类型在列表中。
什么是一个很好/正确的方法来解决这个问题?目前我不得不关闭nhibernate代理。
编辑:我有另外两个解决方案:
- 克隆列表中的所有内容( http://steve-fair-dev.blogspot.com/2007/08/databind-object-does-not-match-target.html ) - 这对我来说不起作用,因为对象不实现ICloneable
- 更改列表中的项目顺序,以便代理不是第一个( http://community.devexpress.com/forums/t/30797.aspx ) - 这是如此的黑客我不认为我可以低于这个低点!
b $ b
由于列表中的代理对象(从延迟加载)导致根本原因,或因为列表不是同构的包含多个类型,即使它们属于同一个类层次结构)?非均匀数据集的问题是已知的限制。请参阅此和这个。
我不认为除了不使用数据绑定来填充网格之外,还有一个解决方案。这很容易,只要它是只读的。
I have a gridview that is bound to the result from an nhibernate query. If the first item in the list is edited the following exception is thrown:
System.Reflection.TargetException: Object does not match target type
It appears that the problem is caused by the fact that databinding can't deal with the first item in the list being a subtype of the other items in the list.
What is a nice / correct way to solve this problem? Currently I have had to turn off nhibernates proxying.
Edit: I have another couple of solutions:
- Clone everything in the list (http://steve-fair-dev.blogspot.com/2007/08/databind-object-does-not-match-target.html) - this doesn't work for me as the object doesn't implement ICloneable
- change the order of items in the list so that the proxy isn't first (http://community.devexpress.com/forums/t/30797.aspx) - this is so hacky, I don't think I can stoop this low!
But none of these feel right though...
Is the root cause due to a proxy object in the list (from lazy loading) or because the list isn't homogeneous (contains multiple types even if they belong to the same class hierarchy)? The problem with non-homogeneous data sets is a known limitation. See this and this.
I don't think there's a solution other than to not use databinding to populate the grid. That's easy enough if it's read-only.
这篇关于NHibernate代理引起数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!