NHibernate-映射字符串外键 [英] NHibernate - Mapping a String Foreign Key

查看:101
本文介绍了NHibernate-映射字符串外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承的旧数据库包含下表:

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

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

Players (
 PlayerId INT PRIMARY KEY,
 Team VARCHAR(30)
)

玩家表中的外键指的是球队名称,而不是teamId.

The foreign key in the players table refers to the team name, rather than teamId.

我尝试使用提包从团队映射到玩家:

I've attempted to map from Team to Players using a bag:

<bag name="Players">
    <key column="Team" foreign-key="Name" />
    <one-to-many class="DataTransfer.Player, DataTransfer" />
</bag>

但是我得到了SqlException: 将varchar值"Arsenal"转换为数据类型int

But I get SqlException: Conversion failed when converting the varchar value 'Arsenal' to data type int

我已经能够使用包在其他区域映射字符串外键,但是在那种情况下,外键是指父表的主键.

I've been able to use a bag to map string foreign keys in other areas, but in those cases the foreign key referred to the primary key of the parent table.

我正在使用NHibernate 2.0.1

I'm using NHibernate 2.0.1

推荐答案

现在,我不确定100%是否可行,但是您是否尝试过多对一的映射关系?

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

也许是这样的:

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

根据NHibernate手册,我认为应该起作用,property-ref是用于映射旧数据的属性,其中外键引用关联表的唯一键而不是主键.这听起来像是您遇到的情况.

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.

这篇关于NHibernate-映射字符串外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆