Fluent NHibernate:如何映射映射类中的"外键"列

con*_*att 1 nhibernate domain-driven-design fluent-nhibernate

我开始用Fluent NHiberate开发,我想知道如何在Mapping类中创建一个定义的'Foreign Key'关系.

这是我的课.这些类与关联表一对一.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的映射文件中,我想在我的Song类SongArtistID列中创建一个定义的外键关系,该列将定义Artist表/类中ArtistID列的外键.

reb*_*ard 7

干得好:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

话虽这么说,使用Automapper配置会更容易.