Reputation: 103
I have a question about how to map a foreign key ID with NHibernate mapping file. eg. I have the following classes:
class Topic {
public long ID {get; set;}
public string Name {get; set;}
}
class Post {
public long ID {get; set;}
public string Title {get; set;}
public string Content {get; set;}
public long TopicID {get; set;} // This is the FK refers to the Topic entity
}
The Post entity doesn't reference the Topic directly, it only references the Topic by the TopicID as show above. However seems that NHibernate <many-to-one
> mapping will only map the Topic as an entity in the Post, but I what I want is the TopicID FK, not the Topic entity. Could anybody help me on this?
Many thanks!
Upvotes: 2
Views: 2298
Reputation: 10289
If you just want a property in your Post mapping that contains the value of the TopicID field, then just map it as a simple property:
<property name="TopicID" column="TopicID" type="long" />
If you want it mapped as an object, then use a many-to-one mapping:
<many-to-one name="Topic" column="TopicID" class="Topic" />
Upvotes: 1
Reputation: 45222
You can just declare the TopicID as a property, and not a many-to-one
.
<property name="TopicID" column="TopicID" type="Int64" />
Upvotes: 0