NHibernate - Mapping a String Foreign Key

后端 未结 4 442
天涯浪人
天涯浪人 2021-01-19 12:48

The legacy database I\'ve inherited contains the following tables:

Teams ( 
 TeamId INT PRIMARY KEY,
 Name VARCHAR(30)
)

Players (
 PlayerId INT PRIMARY KEY         


        
相关标签:
4条回答
  • 2021-01-19 13:15

    Now I'm not 100% sure if this will work but have you tried a many-to-one mapping relationship?

    Maybe something like this:

    <many-to-one name="Players" class="DataTransfer.Player, DataTransfer"
                 column="Name" property-ref="Team" />
    

    I believe that should work, according to the NHibernate manual property-ref is an attribute that is useed for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This sounds like the situation you find yourself in.

    0 讨论(0)
  • 2021-01-19 13:32

    I believe that you would need to use the property-ref attribute to define the field that you will be associating to. The foreign-key attribute is used to generate DDL to create the relationships (if you use that feature).

    0 讨论(0)
  • 2021-01-19 13:34

    Just found this: https://nhibernate.jira.com/browse/NH-1272, seems this is a bug in NHibernate, and it is fixed in 2.1.0Alpha1.

    I have tried it in NHibernate 2.1.0Alpha2, and it works!

    (property-ref should only be in the map tag)

    0 讨论(0)
  • 2021-01-19 13:37

    I think the property-ref attribute exists to solve this problem.

    <bag name="Players">
       <key column="Team" property-ref="Team" />
       <one-to-many class="Player" property-ref="Team" />
    </bag>
    
    0 讨论(0)
提交回复
热议问题