在我正在进行的一个项目中,我们正在使用选项卡。这些标签有内容。多个内容对象可以在一个选项卡上。所以我们可以有一个“汽车”标签,该汽车标签可以显示轿车内容对象、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 TabId ContentObjectId 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);
}
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.