我正在尝试使用Fluent NHibernate Mapping实现双向映射。
域类的代码段:
public class Template
{
public virtual int? Id { get; set; }
public virtual string Title { get; set; }
public virtual TemplateGroup TemplateParentGroup { get; set; }
}
public class TemplateGroup
{
public virtual int? Id {get; set;}
public virtual string Title { get; set; }
public IList<Template> Templates { get; set; }
}
Mapping类的代码片段:
public class TemplateMap : ClassMap<Template>
{
public TemplateMap()
{
Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
Map(x => x.Title).Not.Nullable().Length(150);
References(x => x.TemplateParentGroup).Column("TemplateGroupId").Not.Nullable();
}
}
public class TemplateGroupMap : ClassMap<TemplateGroup>
{
public TemplateGroupMap()
{
Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
Map(x => x.Title).Not.Nullable().Length(150);
HasMany(x => x.Templates).Table("Template").AsBag().Cascade.AllDeleteOrphan();
}
}
但是,当我导出架构时,它会在Template表中生成两个FK列, 以下是Template表的输出SQL:
create table [Template] (
Id INT IDENTITY NOT NULL,
Title NVARCHAR(150) not null,
TemplateDoc VarBinary(MAX) not null,
*TemplateGroupId INT not null,
TemplateGroup_id INT null,*
primary key (Id)
)
因为我已经指定了FK ref。列名称为“TemplateGroupId”,
如何避免生成TemplateGroup_id?
答案 0 :(得分:1)
尝试在HasMany映射
上使用KeyColumnHasMany(x => x.Templates).KeyColumn("TemplateGroupId").Table("Template").AsBag().Cascade.AllDeleteOrphan();