我需要使用流畅的 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 语句的生成吗?