Fluent nHibernate 使用 AsList() 根据 SortOrder 返回 null 对象

发布于 2024-09-11 19:25:09 字数 1744 浏览 1 评论 0原文

在我正在进行的一个项目中,我们正在使用选项卡。这些选项卡有内容。一个选项卡上可以有多个内容对象。因此,我们可以有一个“汽车”选项卡,该汽车选项卡可以显示轿车内容对象、SUV 内容对象和卡车内容对象。用户还可以指定它包含更多或更少的这些对象,并根据需要对它们进行排序。对于我们来说,按照用户规范维护这些内容对象的顺序非常重要。

这是我的映射:

在选项卡映射上:

    HasManyToMany(t => t.ContentObjects) 
        .Table("TabContentObjects") 
        .ParentKeyColumn("TabID") 
        .ChildKeyColumn("ContentObjectID") 
        .AsList(index => index.Column("SortOrder")); 

在 ContentObject 映射上:

    HasManyToMany(c => c.Tabs) 
        .Table("TabContentObjects") 
        .ParentKeyColumn("ContentObjectID") 
        .ChildKeyColumn("TabID") 
        .Cascade.SaveUpdate() 
        .AsList(index => index.Column("SortOrder")); 

关联表是:

TabContentObjectId 标签ID 内容对象ID SortOrder int not null

我能够添加一个内容对象,在一个内容对象中重新排序 选项卡,一切都很好。 nHibernate 正在添加/更新 SortOrder 适当地。当我尝试删除内容时出现问题 目的。当我从这个 contentObject 中删除选项卡时, contentObject.Tabs 看起来很奇怪。

这是我用来删除选项卡的代码:

            //Need to remove each through the contentObject since it is parent 
            foreach (Tab tab in deleteContentObject.Tabs) 
            { 
                tab.RemoveContentObject(deleteContentObject); 
            } 

//in Tab class 
public virtual void RemoveContentObject(TabContentObject item) 
{ 
    if (ContentObjects == null) ContentObjects = new List<TabContentObject>(); 
    ContentObjects.Remove(item); 
} 

如果它是选项卡中唯一的或第一个 (SortOrder = 0) contentObject,我可以删除。如果是第二个内容对象,则 contentObject.Tabs 看起来像 [0] [null]、[1] [Tab]。如果它是选项卡上的第四个 contentObject,则 contentObject.Tabs 看起来像 [0] [null], [1] [空]、[2] [空]、[3][制表符]。因此,根据关联表中的 SortOrder 列,我似乎返回了许多空引用,因此由于空引用而无法删除。我不明白为什么返回这些空值。任何帮助将不胜感激。

In a project I am working on, we are using tabs. These tabs have content. Multiple content objects can be on a tab. So we could have a 'car' tab, and that car tab may display a sedan content object, a suv content object and a truck content object. The user could also specify that it contains more or less of these objects and order them however they want. It is important for us to maintain the order of these content objects to user specification.

Here are my mappings :

On the Tab mapping :

    HasManyToMany(t => t.ContentObjects) 
        .Table("TabContentObjects") 
        .ParentKeyColumn("TabID") 
        .ChildKeyColumn("ContentObjectID") 
        .AsList(index => index.Column("SortOrder")); 

On the ContentObject mapping :

    HasManyToMany(c => c.Tabs) 
        .Table("TabContentObjects") 
        .ParentKeyColumn("ContentObjectID") 
        .ChildKeyColumn("TabID") 
        .Cascade.SaveUpdate() 
        .AsList(index => index.Column("SortOrder")); 

The association table is :

TabContentObjectId
TabId
ContentObjectId
SortOrder int not null

I am able to add a content object, re-order a content object within a
tab, and all is well. nHibernate is adding / updating the SortOrder
appropriately. The problem comes when I am trying to delete a content
object. When I go to delete the tabs from this contentObject,
contentObject.Tabs looks odd.

Here is the code I use to delete the tabs :

            //Need to remove each through the contentObject since it is parent 
            foreach (Tab tab in deleteContentObject.Tabs) 
            { 
                tab.RemoveContentObject(deleteContentObject); 
            } 

//in Tab class 
public virtual void RemoveContentObject(TabContentObject item) 
{ 
    if (ContentObjects == null) ContentObjects = new List<TabContentObject>(); 
    ContentObjects.Remove(item); 
} 

If it is the ONLY, or the first (SortOrder = 0) contentObject within a tab, I can delete. If it is the second content object, contentObject.Tabs look like [0] [null], [1] [Tab]. If it is the fourth contentObject on the tab, contentObject.Tabs looks like [0] [null], [1]
[null], [2] [null], [3][Tab]. So, depending on what the SortOrder column is in the association table, I seem to have many null references returned, and therefore cannot delete due to a null reference. I cannot figure out why these nulls are being returned. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

或十年 2024-09-18 19:25:10

我已经通过以下方法解决了这个问题:

选项卡地图:

        HasManyToMany(t => t.ContentObjects)
            .Table("wfdc_Tab_TabContentObjects")
            .ParentKeyColumn("TabID")
            .ChildKeyColumn("ContentObjectID")
            .Cascade.SaveUpdate()
            .AsList(index => index.Column("SortOrder"));  

内容对象地图:

        HasManyToMany(c => c.Tabs)
            .Table("wfdc_Tab_TabContentObjects")
            .ParentKeyColumn("ContentObjectID")
            .ChildKeyColumn("TabID")
            .Inverse();

所以我只需要一张地图上的 AsList。我还将选项卡设为父级。

删除选项卡:

                foreach (Tab tab in deleteContentObject.Tabs)
                {
                    tab.ContentObjects.Remove(deleteContentObject);
                }

使用此配置,选项卡的内容将按照我们想要的方式排序。 nHibernate 在更新、添加、删除时自动更新排序。

I have resolved this with the following :

Tab Map :

        HasManyToMany(t => t.ContentObjects)
            .Table("wfdc_Tab_TabContentObjects")
            .ParentKeyColumn("TabID")
            .ChildKeyColumn("ContentObjectID")
            .Cascade.SaveUpdate()
            .AsList(index => index.Column("SortOrder"));  

Content Object Map :

        HasManyToMany(c => c.Tabs)
            .Table("wfdc_Tab_TabContentObjects")
            .ParentKeyColumn("ContentObjectID")
            .ChildKeyColumn("TabID")
            .Inverse();

So I only needed the AsList on one map. I also made the Tab the parent.

To remove the tabs :

                foreach (Tab tab in deleteContentObject.Tabs)
                {
                    tab.ContentObjects.Remove(deleteContentObject);
                }

With this configuration, a tab's content is ordered the way we would like. Sorting is automatically updated by nHibernate on updates, add, deletes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文