gpt4 book ai didi

fluent-nhibernate - 如何使用 AutoPersistenceModel 创建具有相同对象的父/子关系

转载 作者:行者123 更新时间:2023-12-04 07:45:32 24 4
gpt4 key购买 nike

我的项目中有以下类(class)


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; }
}

我的数据库表如下:

CREATE TABLE [dbo].[ProductCategory](
[Id] [uniqueidentifier] NOT NULL,
[UrlSlug] [nvarchar](255) NULL,
[Title] [nvarchar](255) NULL,
[IsActive] [bit] NULL,
[ProductCategory_id] [uniqueidentifier] NULL -- this is the parent category id
)

我试图让一个类别成为另一个类别的子类别,显然,父类别可以有多个类别。

我在让 AutoPersistenceModel 工作时遇到了麻烦。这就是我的映射所拥有的。

.ForTypesThatDeriveFrom(map =>
{
map.HasMany(productCategory => productCategory.ProductCategories).WithForeignKeyConstraintName("ProductCategory_id");
map.HasOne(productCategory => productCategory.ParentCategory).WithForeignKey("ProductCategory_id");
});

我尝试了一些对我不起作用的不同方法。它似乎正确地映射了 HasMany。但是当数据库中的 ParentCategory_id 为空时, HasOne 最终成为它自己,而不是正确的父实体,或者什么都没有(空)

最佳答案

Anthony,试着把 has one 改成这个。

map.References(productCategory => productCategory.ParentCategory).WithColumns("ProductCategory_id").FetchType.Select();


has one 是一对一的关系,您希望将另一个类作为父类引用。

关于fluent-nhibernate - 如何使用 AutoPersistenceModel 创建具有相同对象的父/子关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2242185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com