c# - 如何在 NHibernate 中创建父级并同时向其添加子级?

标签 c# nhibernate

当我尝试创建集合并同时向其中添加类别时,我不断收到空引用。我是不是忘记了什么?还是我做错了?

private static void SamepleMethod(ICollectionRepository collectionRepo)
    {
        Collection collection = new Collection { CollectionName = "Collection" };

        Category category = new Category { Collection = collection, CategoryName = "Category" };

        category.SetCollection(collection);
        collection.AddCategory(category);

        collectionRepo.Save(collection);
    }

集合:

public class Collection
{
    public virtual int? Id { get; set; }
    public virtual string CollectionName { get; set; }
    public virtual ICollection<Category> Categories { get; set; }

    public virtual void AddCategory(Category category)
    {
        Categories.Add(category);
        category.Collection = this;
    }
}

类别:

public class Category
{
    public virtual int? Id { get; set; }
    public virtual string CategoryName { get; set; }
    public virtual Collection Collection { get; set; }

    public virtual void SetCollection(Collection collection)
    {
        Collection = collection;
        collection.Categories.Add(this);
    }
}

集合.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               namespace="ACME.Model"
               assembly="ACME.Model">

<class name="Collection" table="Collections" >

<id name="Id" column="CollectionId" type="int" >
  <generator class="identity" />
</id>

<set name="Categories" inverse="true" cascade="all">
  <key column="CollectionId" />
  <one-to-many class="Category"></one-to-many>
</set>

<property name="CollectionName">
  <column name="CollectionName" sql-type="nvarchar(50)" />
</property>

类别.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               namespace="ACME.Model"
               assembly="ACME.Model">

<class name="Category" table="Categories" >

<id name="Id" column="CategoryId" type="int" >
  <generator class="identity" />
</id>

<many-to-one name="Collection"
             class="Collection"
             column="CollectionId"
             not-null="true" />

<property name="CategoryName">
  <column name="CategoryName" sql-type="nvarchar(50)" />
</property>

最佳答案

您从未初始化过 Collection 类中的 Categories 属性,因此它仍保持默认值 null...
您可能希望在构造函数中将其初始化为新集合。

另外,我统计了至少 3 行不同的代码行,您可以在其中将类别与集合关联起来。
我相信一次就足够了:)(我最喜欢在 AddCategory 方法中,或者在 Category 的构造函数中)。

另一件小事 - hbm 映射文件已成为过去;如今,在代码中进行映射更加方便(并且不易出错)。
您可以使用旧扩展 fluent nHibernate 来完成此操作,或者新官方mapping-by-code ,这也非常好,但可惜缺乏文档。

关于c# - 如何在 NHibernate 中创建父级并同时向其添加子级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12146021/

相关文章:

c# - BigInteger 不能表示无穷大

c# - 如何在 PropertyValidator 重写的 IsValid 中等待方法?

c# - 在这种情况下解决代码分析 "CA2000"规则?

nhibernate - 找到对集合的共享引用

nhibernate - Automapper 和 NHibernate 延迟加载

c# - 序列化为不涉及属性的json时如何忽略属性

c# - Xamarin 形成要流式传输的图像

c# - 具有多个包含列表的表的 JoinOverQuery

c# - 在流畅的 nHibernate 中使用通用存储库模式

oracle - 如何在连接错误时清除 ODP.NET 连接池?