Fluent NHibernate : How to map a 'Foreign Key' column in the mapping class

前端 未结 2 841
长发绾君心
长发绾君心 2020-12-18 16:10

I\'m starting to develop with Fluent NHiberate, and I was wondering how I create a defined \'Foreign Key\' relationship in my Mapping class.

Here\'s my class. These

相关标签:
2条回答
  • 2020-12-18 16:39

    Here you go:

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

    That being said, it's easier using the Automapper configuration.

    0 讨论(0)
  • I have things to work with identifiers via next (tested on my entities, here just renamed things and no run was done, please consider it as patter to try):

    References<Artist>(x => x.SongArtistID ).Column("SongArtistID").ForeignKey("ArtistID");
    
    0 讨论(0)
提交回复
热议问题