我需要使用流畅的NHibernate在Project和ProjecSettings之间实现一对一的关联:
public class ProjectMap : ClassMap<Project>
{
public ProjectMap()
{
Id(x => x.Id)
.UniqueKey(MapUtils.Col<Project>(x => x.Id))
.GeneratedBy.HiLo("NHHiLoIdentity", "NextHiValue", "1000",
string.Format("[EntityName] = '[{0}]'", MapUtils.Table<Project>()))
.Not.Nullable();
HasOne(x => x.ProjectSettings)
.PropertyRef(x => x.Project);
}
}
public class ProjectSettingsMap : ClassMap<ProjectSettings>
{
public ProjectSettingsMap()
{
Id(x => x.Id)
.UniqueKey(MapUtils.Col<ProjectSettings>(x => x.Id))
.GeneratedBy.HiLo("NHHiLoIdentity", "NextHiValue", "1000",
string.Format("[EntityName] = '[{0}]'", MapUtils.Table<ProjectSettings>()));
References(x => x.Project)
.Column(MapUtils.Ref<ProjectSettings, Project>(p => p.Project, p => p.Id))
.Unique()
.Not.Nullable();
}
}
这会产生以下用于项目设置的SQL:
CREATE TABLE ProjectSettings (
Id bigint PRIMARY KEY NOT NULL,
Project_Project_Id bigint NOT NULL UNIQUE,
/* Foreign keys */
FOREIGN KEY (Project_Project_Id)
REFERENCES Project()
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
我想要实现的是为FOREIGN KEY(Project_Project_Id)设置ON DELETE CASCADE,这样当通过sql查询删除项目时,它的设置也会被删除。我怎样才能做到这一点?
编辑:我知道Cascade.Delete()选项,但这不是我需要的。有没有办法拦截FK语句生成?
答案 0 :(得分:1)
似乎Fluent-NHibernate无法做到这一点。
你可以这样做:
HasOne(x => x.ProjectSettings).PropertyRef(x => x.Project).Cascade.Delete();
这将按预期工作,但DDL不会更改,FNH将以编程方式处理级联,而不是使用数据库引擎。