代码之家  ›  专栏  ›  技术社区  ›  contactmatt

Fluent NHibernate:如何在mapping类中映射“外键”列

  •  1
  • contactmatt  · 技术社区  · 16 年前

    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);
        }
    }
    

    2 回复  |  直到 16 年前
        1
  •  7
  •   Patrick Quirk    12 年前

    干得好:

    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

        2
  •  4
  •   Dzmitry Lahoda Adam    10 年前

    References<Artist>(x => x.SongArtistID ).Column("SongArtistID").ForeignKey("ArtistID");