Fluent NHibernate:如何映射映射类中的'外键'列
问题描述:
我开始用Fluent NHiberate进行开发,我想知道如何在Mapping类中创建定义的“外键”关系。Fluent NHibernate:如何映射映射类中的'外键'列
这是我的课。这些类与关联表是一对一的。
public class Song
{
public virtual int SongID{ get; private set; } //Primary Key
public virtual int SongArtistID { get; set; } //Foreign Key mapping to 'Artist.ArtistID'
public virtual string Title { get; set; }
}
public class Artist
{
public virtual int ArtistID{ get; private set; } //Primary Key
public virtual int ArtistName{ get; set; }
}
public class SongMapping : ClassMap<Song>
{
SongMapping()
{
Id(c => c.SongID);//.GeneratedBy.HiLo("sermon"); is HiLo generator good?
Map(c => c.SermonArtistID).Not.Nullable(); //How is this mapped to 'Artist.ArtistID'??
Map(c => c.Title).Not.Nullable().Length(50);
}
}
在我的映射文件,我想创建我的歌类SongArtistID列定义外键关系,这将定义一个外键在艺术家表/类ArtistID列。
答
在这里你去:
public class Song
{
public virtual int Id { get; private set; }
public virtual Artist Artist { get; set; }
public virtual string Title { get; set; }
public class SongMap : ClassMap<Song>
{
SongMap()
{
Id(c => c.Id);
References(c => c.Artist); // Yes, that's all.
Map(c => c.Title).Not.Nullable().Length(50);
}
}
}
这就是说,它的使用Automapper configuration容易。
答
我有事情通过接下来的标识符的工作(我的实体测试,这里只是改名的事情,并没有运行已完成,请把它作为图案尝试):
References<Artist>(x => x.SongArtistID).Column("SongArtistID").ForeignKey("ArtistID");
啊,没关系。在我的Song课程中没有Artist类型,我感到困惑。谢谢。 – contactmatt 2010-09-11 16:22:56
thnx为答案,答案的最好部分是automapper配置 – 2015-11-14 07:56:19