Mapping Composite keys in Fluent NHibernate

前端 未结 4 1589
清歌不尽
清歌不尽 2020-12-14 05:39

I am new to fluent NHibernate. Now I face one problem with mapping composite keys. Can anyone point out the URL or sample please?

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 06:43

    There may be a need for entities with composite identifiers, entities that map to tables which have composite primary keys, composed of many columns. The columns that make up this primary key are usually foreign keys to another tables.

    public class UserMap : ClassMap
    {      
       public UserMap()
       {
            Table("User");
    
            Id(x => x.Id).Column("ID");
    
            CompositeId()
              .KeyProperty(x => x.Id, "ID")
              .KeyReference(x => x.User, "USER_ID");
    
            Map(x => x.Name).Column("NAME");               
    
            References(x => x.Company).Column("COMPANY_ID").ForeignKey("ID");
        }
    }
    

    For more reference : http://www.codeproject.com/Tips/419780/NHibernate-mappings-for-Composite-Keys-with-associ

提交回复
热议问题