fluent nhibernate foreign key with 2 columns mappi

2019-09-02 11:26发布

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?

1条回答
forever°为你锁心
2楼-- · 2019-09-02 11:52

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

查看更多
登录 后发表回答