Fluent nhibernate 如何使用AutoPersistenceModel与同一对象创建父/子关系
在我的项目中,我有以下课程Fluent nhibernate 如何使用AutoPersistenceModel与同一对象创建父/子关系,fluent-nhibernate,parent-child,Fluent Nhibernate,Parent Child,在我的项目中,我有以下课程 public class ProductCategory { public virtual Guid Id { get; set; } public virtual string UrlSlug { get; set; } public virtual string Title { get; set; } public virtual bool IsActive { get; set; } public virtual IList<Prod
public class ProductCategory
{
public virtual Guid Id { get; set; }
public virtual string UrlSlug { get; set; }
public virtual string Title { get; set; }
public virtual bool IsActive { get; set; }
public virtual IList<Product> Products { get; set; }
public virtual ProductCategory Parent { get; set; }
public virtual IList<ProductCategory> Categories { get; set; }
}
我试图允许一个类别是另一个类别的子类别,显然,父类别可以有多个类别
我的AutoPersistenceModel无法正常工作。这就是我的映射
.ForTypesThatDeriveFrom(map =>
{
map.HasMany(productCategory => productCategory.ProductCategories).WithForeignKeyConstraintName("ProductCategory_id");
map.HasOne(productCategory => productCategory.ParentCategory).WithForeignKey("ProductCategory_id");
});
我尝试了一些不同的方法,但都不管用。它似乎正确地映射了HasMany。但是当数据库中的ParentCategory_id为null时,HasOne就变成了它自己,而不是正确的父实体,或者什么都没有(null)我们在一个项目中做了类似的事情,这是我们使用的约定:
public class ProductCategoryReferencingConvention : IHasManyToManyConvention
{
public bool Accept(IManyToManyPart target)
{
return target.EntityType == typeof (ProductCategory);
}
public void Apply(IManyToManyPart target)
{
target.WithParentKeyColumn("ProductCategory_id");
}
}
安东尼,试着把这个换成这个 has-one是一对一的关系,您希望引用另一个类作为父类
public class ProductCategoryReferencingConvention : IHasManyToManyConvention
{
public bool Accept(IManyToManyPart target)
{
return target.EntityType == typeof (ProductCategory);
}
public void Apply(IManyToManyPart target)
{
target.WithParentKeyColumn("ProductCategory_id");
}
}
map.References(productCategory => productCategory.ParentCategory).WithColumns("ProductCategory_id").FetchType.Select();