如何使用 Fluent NHibernate 映射自引用文件夹层次结构?
我有一个由以下类表示的文件夹层次结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须指定映射覆盖上的列。
I think you will have to specify the columns on your mapping overrides.