如何通过 Fluent nHibernate 将记录插入自引用 table?
How to insert records into a Self referencing table via Fluent nHibernate?
我用 Fluent nhibernate 创建了一个自引用类别 Table [MsSQL 2012],在测试期间,我只是通过 SSMS 手动添加数据; 'but now I wish to do it through code, I’m a little lost?'
public class CategoryMap : ClassMap<CategoryPartRecord> {
public CategoryMap() {
Table("Ceck_CategoryPartRecord");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
//Relationships-
// Recursive:
// [Category]---<[Category]
References(x => x.ParentCategory)
HasMany(x => x.Categories)
.Inverse()
.Cascade.All();
}
}
我试过两种方法:
using (var transactionB = session.BeginTransaction())
{
CategoryPartRecord Cat1 = new CategoryPartRecord() { Name = "Exterior" };
CategoryPartRecord Cat2 = new CategoryPartRecord() { Name = "Interior" };
CategoryPartRecord Cat3 = new CategoryPartRecord() { Name = "Garden" };
CategoryPartRecord Cat4 = new CategoryPartRecord() { Name = "Paint" };
Cat1.Categories.Add(Cat4);
session.Save(Cat1);
session.Save(Cat2);
session.Save(Cat3);
transactionB.Commit();
}
.
using (var transactionBB = session.BeginTransaction())
{
var Cat = session.Load<CategoryPartRecord>(1); //Contains "Exterior"
CategoryPartRecord Cat2 = new CategoryPartRecord() { Name = "Paint" };
Cat.Categories.Add(Cat2);
session.Save(Cat);
transactionBB.Commit();
}
两者都添加了包含 "Paint" 的新记录,但 link 关系不正确,因为 "CategoryPartRecord_id" 始终为 NULL -- 我是否遗漏了一些非常明显的东西,因为两者都不起作用?
嗯,大胆猜测 - 递归的反转会更有趣,所以要消除任何有趣的行为,请将您的映射更改为这个,让我知道您的进展如何?
References(x => x.ParentCategory).Cascade.SaveUpdate();
HasMany(x => x.Categories)
.Cascade.SaveUpdate();
我用 Fluent nhibernate 创建了一个自引用类别 Table [MsSQL 2012],在测试期间,我只是通过 SSMS 手动添加数据; 'but now I wish to do it through code, I’m a little lost?'
public class CategoryMap : ClassMap<CategoryPartRecord> {
public CategoryMap() {
Table("Ceck_CategoryPartRecord");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
//Relationships-
// Recursive:
// [Category]---<[Category]
References(x => x.ParentCategory)
HasMany(x => x.Categories)
.Inverse()
.Cascade.All();
}
}
我试过两种方法:
using (var transactionB = session.BeginTransaction())
{
CategoryPartRecord Cat1 = new CategoryPartRecord() { Name = "Exterior" };
CategoryPartRecord Cat2 = new CategoryPartRecord() { Name = "Interior" };
CategoryPartRecord Cat3 = new CategoryPartRecord() { Name = "Garden" };
CategoryPartRecord Cat4 = new CategoryPartRecord() { Name = "Paint" };
Cat1.Categories.Add(Cat4);
session.Save(Cat1);
session.Save(Cat2);
session.Save(Cat3);
transactionB.Commit();
}
.
using (var transactionBB = session.BeginTransaction())
{
var Cat = session.Load<CategoryPartRecord>(1); //Contains "Exterior"
CategoryPartRecord Cat2 = new CategoryPartRecord() { Name = "Paint" };
Cat.Categories.Add(Cat2);
session.Save(Cat);
transactionBB.Commit();
}
两者都添加了包含 "Paint" 的新记录,但 link 关系不正确,因为 "CategoryPartRecord_id" 始终为 NULL -- 我是否遗漏了一些非常明显的东西,因为两者都不起作用?
嗯,大胆猜测 - 递归的反转会更有趣,所以要消除任何有趣的行为,请将您的映射更改为这个,让我知道您的进展如何?
References(x => x.ParentCategory).Cascade.SaveUpdate();
HasMany(x => x.Categories)
.Cascade.SaveUpdate();