简体   繁体   中英

Two FKs as one PK in NHibernate-Mapping?

My database looks like this

 Role                      Role_AppUser              AppUser
 -------------             -------------             -------------
 |Id         | <-PK        |Id         | <-FK        |UserId     | <-PK
 |Name       |             |UserId     | <-FK        |PreName    |
 |Description|             |xy         |             |SurName    |        
 -------------             -------------             -------------

Now I want those 2 FKs of Role_Appuser table to become a combined PK. How can I do this with my NHibernate mapping? I'm using mapping-by-code.

Thanks in advance.

In the mapping you can use a CompositeId:

public class Role_AppUserMap : ClassMap<Role_AppUser>
{
   public Role_AppUserMap()
   {
       Table("Role_AppUser")
       CompositeId()
         .KeyProperty(x=>x.RoleId, "Id")
         .KeyProperty(x=>x.UserId, "UserId");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM