非法访问加载集合错误

发布于 2024-08-26 02:39:54 字数 1169 浏览 3 评论 0原文

我收到错误

非法访问加载集合

当我尝试获取属于特定产品的变体列表时, 。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

川水往事 2024-09-02 02:39:54

好吧,虽然很蠢,但我终于解决了问题。

数据库中的索引列 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 always NULL. 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!

绾颜 2024-09-02 02:39:54

问题解决了!我在添加具有变体的产品时遇到了另一个问题,因此我在控制器中更改了此智能。然后我遇到了映射问题,所以我按如下方式更改了映射,一切正常!

    <list name="Variants" lazy="false" cascade="all" inverse="false">
  <key column="ProductId" />
  <index column="Ordinal" />
  <one-to-many class="Plugin.Product.Business.Entities.Variant, Plugin.Product" />
</list>

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!

    <list name="Variants" lazy="false" cascade="all" inverse="false">
  <key column="ProductId" />
  <index column="Ordinal" />
  <one-to-many class="Plugin.Product.Business.Entities.Variant, Plugin.Product" />
</list>
南风起 2024-09-02 02:39:54

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).

小巷里的女流氓 2024-09-02 02:39:54

我遇到了这个问题,这不是映射问题,而是实际上的数据问题。 我们的集合中收到了太多的数据,但我们还是得到了这个异常,而不是更有用的东西。

我预计我的收藏包含 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.

等你爱我 2024-09-02 02:39:54

多对一属性未在其他映射类中正确映射,因此不会带来与其关联的结果。

基本上我删除了这一行:map.PropertyRef("Codigo");
并且已经正常运行了。

 ManyToOne(x => x.Menu, map => 
            {
                    map.Column("COD_MENU");
                    //map.PropertyRef("Codigo");
                    map.NotNullable(true);
                    map.Cascade(Cascade.None);
            });

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.

 ManyToOne(x => x.Menu, map => 
            {
                    map.Column("COD_MENU");
                    //map.PropertyRef("Codigo");
                    map.NotNullable(true);
                    map.Cascade(Cascade.None);
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文