如何使用 Fluent NHibernate 映射自引用文件夹层次结构?

发布于 2024-10-04 05:21:33 字数 1941 浏览 3 评论 0原文

我有一个由以下类表示的文件夹层次结构:

public class Folder
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Folder ParentFolder { get; set; }
    public virtual ICollection<Folder> SubFolders { get; set; }
}

换句话说,每个 Folder 可以属于 ParentFolder,并且在下面有 SubFolders它。我正在使用 Fluent NHibernate 的 Automapper 并使用 SchemaExport 自动生成数据库架构。当我尝试保存一些测试文件夹时,我得到下表:

Id | Name                        | ParentFolder_id | Folder_id
----------------------------------------------------------------
1  | Root Folder (has children)  | NULL            | NULL
2  | Root Folder (no children)   | NULL            | NULL
3  | Sub Folder                  | 1               | NULL
4  | Sub Sub Folder              | 2               | NULL

到目前为止一切顺利,ParentFolder_id列设置正确,尽管我不知道为什么它创建了另一个Folder_id代码> 列。现在,当我尝试运行以下代码时:

using (var session = SessionFactory.OpenSession())
{
    // I'm using NHibernate 3
    var rootFolder = session.Query<Folder>()
                            .Where(x => x.Name.StartsWith("root").First();

    Console.WriteLine(rootFolder.SubFolders.Count());
}

返回的计数为0,并执行以下SQL:

SELECT count(Id) FROM [Folder] WHERE Folder_id = 1

该SQL语句是错误的。它应该这样做:

SELECT count(Id) FROM [Folder] WHERE ParentFolder_id = 1

任何人都可以告诉我为什么 Fluent NHibernate 创建额外的 Folder_id 列并对其进行查询,以及我如何修复它以便它正确查询 ParentFolder_id列代替?我尝试了以下覆盖但没有成功:

public class FolderOverride : IAutoMappingOverride<Folder>
{
    public void Override(AutoMapping<Folder> mapping)
    {
        mapping.HasMany(x => x.SubFolders).Inverse(); // I thought inverse might fix it, but no dice
        mapping.References(x => x.ParentFolder);
    }
}

I have a folder hierarchy represented by the following class:

public class Folder
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Folder ParentFolder { get; set; }
    public virtual ICollection<Folder> SubFolders { get; set; }
}

In other words, each Folder can belong to a ParentFolder, as well as have SubFolders under it. I'm using Fluent NHibernate's Automapper and automatically generating the database schema using SchemaExport. I get the following table when I try to save some test folders:

Id | Name                        | ParentFolder_id | Folder_id
----------------------------------------------------------------
1  | Root Folder (has children)  | NULL            | NULL
2  | Root Folder (no children)   | NULL            | NULL
3  | Sub Folder                  | 1               | NULL
4  | Sub Sub Folder              | 2               | NULL

So far so good, the ParentFolder_id column is being set correctly, although I don't know why it created another Folder_id column. Now, when I try to run the following code:

using (var session = SessionFactory.OpenSession())
{
    // I'm using NHibernate 3
    var rootFolder = session.Query<Folder>()
                            .Where(x => x.Name.StartsWith("root").First();

    Console.WriteLine(rootFolder.SubFolders.Count());
}

The count returned is 0, and the following SQL is executed:

SELECT count(Id) FROM [Folder] WHERE Folder_id = 1

This SQL statement is wrong. It should be doing:

SELECT count(Id) FROM [Folder] WHERE ParentFolder_id = 1

Can anyone tell me why Fluent NHibernate is creating the extra Folder_id column and querying on it, and how I can fix it so that it properly queries the ParentFolder_id column instead? I've tried the following override with no luck:

public class FolderOverride : IAutoMappingOverride<Folder>
{
    public void Override(AutoMapping<Folder> mapping)
    {
        mapping.HasMany(x => x.SubFolders).Inverse(); // I thought inverse might fix it, but no dice
        mapping.References(x => x.ParentFolder);
    }
}

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

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

发布评论

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

评论(1

强者自强 2024-10-11 05:21:33

我认为您必须指定映射覆盖上的列。

mapping.HasMany(x => x.SubFolders).KeyColumn("ParentFolder_id");

I think you will have to specify the columns on your mapping overrides.

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