Fluent NHibernate 双向映射 - 产生两个相似的引用列
我正在尝试使用 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; }
}
映射类中的代码片段:
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();
}
}
但是,当我导出架构时,它会在模板表中产生两个 FK 列, 以下是模板表的输出 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 引用。列名称为“TemplateGroupId”,
如何避免生成TemplateGroup_id?
I am trying to implement Bidirectional Mapping with Fluent NHibernate Mapping.
Code snippet from Domain classes:
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; }
}
Code snippet from Mapping classes:
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();
}
}
However, when I am exporting schema, it results in two FK columns in Template table,
Here is the output SQL for Template table:
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)
)
As I am already specifying the FK ref. column name as "TemplateGroupId",
How can I avoid TemplateGroup_id to be generated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在 HasMany 映射上使用 KeyColumn
Try using the KeyColumn on the HasMany mapping