简体   繁体   中英

fluent nhibernate foreign key with 2 columns mapping

I have to an existing schema and I want to map it with nhibernate.

entities / table schema:

post {
  pk_id
  prod_id
  prod_internid
  title
}

tag {
  pk_t_id
  prod_id
  prod_internid
  name
}

A post can have multiple tags and there is a foreign key contraint from the tag to the post table with the two columns prod_id and prod_internid.

I've tried this:

PostMap {
  // tags is a list
  HasMany(x => x.tags).KeyColumns.Add("prod_id", "prod_internid");
}

TagMap {
  References(x => x.post).Columns("prod_id", "prod_internid");//.ForeignKey();
}

I get this error:

NHibernate.FKUnmatchingColumnsException: Foreign key (FK98806C8630C05A78:tag [prod_id, prod_internid])) must have same number of columns as the referenced primary key (post [pk_id])

How can I map it the right way?

I don't think this functionality is currently supported in NHibernate but it is in Hibernate. Seems like you or someone would need to port it over. Take a look at this NH Issue:

https://nhibernate.jira.com/browse/NH-1722

I also found this previous StackOverflow article regarding this:

many-to-one with multiple columns

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