NHibernate:将用户类型对象映射到单独的表
让我们从这个映射开始:
<component name="Location">
...
<property name="Settings" type="JsonUserType,...">
<column name="LocationSettingsType" />
<column name="LocationSettingsData" />
</property>
</component>
这映射到
TABLE Primary (
...
LocationSettingsType,
LocationSettingsData
...
)
现在
class Location {
...
object Settings { get; set; }
}
,我想将设置提取到一个单独的表中(因为它们很少出现在此处)。
所以我得到
TABLE Primary (
...
LocationSettingsId,
...
)
TABLE Settings (
Id,
Type,
Data
)
我可以保持我的 C# 类相同吗?
更新: 这不是多对一关系。与之前一样,每个位置都有零个或一个设置,并且每个设置最多属于一个位置。
Let's start with this mapping:
<component name="Location">
...
<property name="Settings" type="JsonUserType,...">
<column name="LocationSettingsType" />
<column name="LocationSettingsData" />
</property>
</component>
This maps to
TABLE Primary (
...
LocationSettingsType,
LocationSettingsData
...
)
and
class Location {
...
object Settings { get; set; }
}
Now, I want to extract settings into a separate table (because they are seldom here).
So I get
TABLE Primary (
...
LocationSettingsId,
...
)
TABLE Settings (
Id,
Type,
Data
)
Can I keep my C# classes the same?
Update: This is not a many-to-one relationship. As before, each location has zero or one settings, and each settings belong to at most one location.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信与此最接近的是
映射元素;详细信息在这篇文章中进行了解释。
I believe the closest thing that exists to this is the
<map>
mapping element; details are explained in this article.如果您希望主表和设置表上存在一对多关系,则必须首先设置外键约束。然后,您将使用 XML 中的
bag
属性来映射您的表。每个表都会有一个实体
。另请参阅 NHibernate/FluentNHibernate 属性包 上的此问题。
我还建议您购买NHibernate 2 for Beginners一书。这对我帮助很大。
If you want a one to many relationship on the Primary and Settings tables, you'll have to set a foreign key constraint first. Then you'll use the
bag
property in XML to map your tables. You will have anentity
for each table.See also this question on NHibernate/FluentNHibernate Property Bag.
I also recommend you purchase the NHibernate 2 for Beginners book. It helped me alot.
这是一个老问题,但我遇到了同样的问题,并寻求我来到这里的解决方案。
component
元素可以将多个列映射到多个对象模型。join
元素可以将多个表映射到一个对象模型。主要问题是,虽然组件无法映射模型所属的不同表中的列,但联接无法将不同表列映射到不同的对象模型。
我找到的解决方案是使用两者来实现不同表的映射列到多个对象:
参考:
NHibernate 连接映射元素
NHibernate 组件映射元素
This is an old question but i had the same issue and looking to a solution I came here.
the
component
element can map several columns to several object models.the
join
element can map several tables to an object model.The main problem is that while the
component
cannot map columns from a different table where the model belong, thejoin
cannot map the different table columns to a different object model.The solution I found is to use both to achieve the map column of a different table to several object:
Reference:
NHibernate Join mapping element
NHibernate Component mapping element