非法访问加载集合错误
我收到错误
非法访问加载集合
当我尝试获取属于特定产品的变体列表时, 。 NHibernate 映射如下
<list name="Variants" lazy="false" cascade="save-update" inverse="false" table="PluginProduct_ProductVariant">
<key column="ProductId" />
<index column="Ordinal" />
<one-to-many class="Plugin.Product.Business.Entities.Variant, Plugin.Product" />
</list>
我已经尝试按照本网站其他主题中的建议更改惰性和逆属性,但它们没有成功。
我正在将 NHibernate 与 ASP.NET MVC 结合使用,并且我正在尝试循环遍历我认为的变体集合。视图正在调用以下方法
public ActionResult ShowProduct()
{
var id = new Guid(PluginData.PageParameters["Id"]);
var variant = _variantService.GetVariantById(id);
var product = variant.Product;
return PluginView("ShowProduct.ascx", product);
}
上面的代码运行没有任何问题。但是,当我在返回视图之前进行调试时,我发现产品包含的变体列表为空。当我打开更详细的调试信息时,它会向我显示收集错误。
从我的网络应用程序的角度来看,我正在尝试执行以下操作
<%
foreach (var variant in Model.Variants)
{%>
kleur: <%= variant.Color %>
van: <%= variant.FromPrice %> voor: <%= variant.Price %>
<%} %>
I'm getting the error
Illegal access to loading collection
when I'm trying to get a list of variants belonging to a certain product. The NHibernate mapping is as below
<list name="Variants" lazy="false" cascade="save-update" inverse="false" table="PluginProduct_ProductVariant">
<key column="ProductId" />
<index column="Ordinal" />
<one-to-many class="Plugin.Product.Business.Entities.Variant, Plugin.Product" />
</list>
I already tried chancing the laziness and inverse properties as suggested in other topics on this site, but they didn't do the trick.
I'm using NHibernate in combination with ASP.NET MVC and and I'm trying to loop through a collection of variant in my view. The view is calling the following method
public ActionResult ShowProduct()
{
var id = new Guid(PluginData.PageParameters["Id"]);
var variant = _variantService.GetVariantById(id);
var product = variant.Product;
return PluginView("ShowProduct.ascx", product);
}
The above code runs without any problems. But when I debug just before returning the view I see that the list of variants which the product contains is empty. When I open more detailed debug information it's showing me the collection error.
In the view of my web application I'm trying to do the following
<%
foreach (var variant in Model.Variants)
{%>
kleur: <%= variant.Color %>
van: <%= variant.FromPrice %> voor: <%= variant.Price %>
<%} %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,虽然很蠢,但我终于解决了问题。
数据库中的索引列
Ordinal
未获取正确的值,因此它始终为NULL
。这导致了错误,因为 NHibernate 无法找到用于创建列表的索引列。不幸的是,我花了很多时间,但很高兴我解决了这个问题!
Okay, very stupid, but I finally got the problem solved.
The index column
Ordinal
in the database wasn't getting the correct values so it was alwaysNULL
. This caused the error because NHibernate couldn't find an index column to create the list on.Cost me a lot of time unfortunately, but glad I got it solved!
问题解决了!我在添加具有变体的产品时遇到了另一个问题,因此我在控制器中更改了此智能。然后我遇到了映射问题,所以我按如下方式更改了映射,一切正常!
Got the problem solved! I ran into an other problem adding a product with a variant so i changed this intelligence in my controller. Then i ran into a problem with the mapping so i changes the mapping as below and it all worked!
inverse="true" 是最常用的,因为它意味着另一个端点是在一对多关联中拥有密钥的端点(多方拥有一侧的外键)。
inverse="true" is the most commonly used, because it means that the other endpoint is the one that has the key in one to many associations (the many side has a foreign key to the one side).
我遇到了这个问题,这不是映射问题,而是实际上的数据问题。 我们的集合中收到了太多的数据,但我们还是得到了这个异常,而不是更有用的东西。
我预计我的收藏包含 10-15 条记录,但实际上有大约 400 万条记录。
I got this problem and it wasn't a mapping problem but actually a data problem. We received way too much data in our collections, but we got this exception anyway instead of something more useful.
I expected my collection to contain 10-15 records but it had some 4 million records.
多对一属性未在其他映射类中正确映射,因此不会带来与其关联的结果。
基本上我删除了这一行:
map.PropertyRef("Codigo");
并且已经正常运行了。
The many to one property isn´t mapped corretly in the other map class, so it will not bring results to relate it to.
Basically I deleted the line:
map.PropertyRef("Codigo");
And it has worked normally.