简体   繁体   中英

How to join two tables together with columns that aren't keys (in NHibernate)?

I have one table that has an Id (PK) and another table with a bunch of information. There will be a one-to-many relationship from the first table to some data in the second table, but there's no FK's anywhere. So I want to do an inner join on table1.Id with table2.OtherId.

I've got my Model set up with an IList and in my table1 map I have:

HasMany(x => x.Properties).KeyColumn("table2id");

but I guess since it doesn't know what to join that column with in the first table, it won't work. I know the table2 model is working correctly because I've tried it on its own and I get all the correct data. It's just this HasMany thing that's getting messed up.

I've looked at the other "related" questions here on SO but I can't see anything that works for me.

You don't have to have a FK relationship but the column specified in KeyColumn must be the primary key of table2. I don't know of a workaround.

So it turned out that I needed to have References in the Table2 mapping onto Table1. And then simply HasMany(x => x.Properties).Inverse.Cascade.None() in the Table1 Mapping sorted it out.

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