NHibernate 将 1 个对象映射到 2 个表
我们希望使用 NHibernate 作为应用程序中的持久层。我们还使用 Fluent NHibernate 进行映射。
我们从第三方获取人员数据,我们需要将数据保存到我们的数据库中。在代码中将所有属性放在一个对象上效果更好,但将数据保存到数据库中的 2 个表中更有意义。
我们的对象看起来像这样:
public class Person
{
public virtual long VersionNumber { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string IdentificationNumber { get; set; }
}
我们的数据库表看起来像这样:
CREATE TABLE PersonVersion (
[PK] VersionNumber bigint NOT NULL,
[FK] PersonDemographicsId int NOT NULL
)
CREATE TABLE PersonDemographics (
[PK] PersonDemographicsId int NOT NULL,
IdentificationNumber nvarchar(9) NOT NULL,
FirstName nvarchar(100) NOT NULL,
LastName nvarchar(100) NOT NULL
)
当我们收到新数据时,版本号可以更改,但其余的人口统计数据可能是相同的。我们需要 NHibernate 做的是将新记录保存到 PersonVersion
表中,该表链接到现有的 PersonDemographics
记录。如果人口统计数据发生变化,那么我们将在两个表中创建新记录。但大多数时候,一旦我们下载了初始数据,人口统计数据就不会像版本号那样频繁变化。我们需要跟踪所有版本号,因此需要创建新的 PersonVersion
记录。
我们如何使用 NHibernate 并使用 Fluent NHibernate 进行映射来完成此任务?
另外,正如您所看到的,我们的 Person 对象当前没有 PersonDemographicsId
,因为我们的应用程序根本不需要它;它只是数据库中需要的表关系的ID。为了在 NHibernate 中正确映射它,我们是否必须在 Person 对象上添加一个 PersonDemographicsId
属性?
感谢您的帮助!
We want to use NHibernate as our persistence layer in our application. We are also using Fluent NHibernate for the mappings.
We get Person data from a 3rd party and we need to save the data to our database. It works better in the code to have all properties on one object, but it makes more sense to save the data to 2 tables in our database.
Our object looks like this:
public class Person
{
public virtual long VersionNumber { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string IdentificationNumber { get; set; }
}
Our database tables look like this:
CREATE TABLE PersonVersion (
[PK] VersionNumber bigint NOT NULL,
[FK] PersonDemographicsId int NOT NULL
)
CREATE TABLE PersonDemographics (
[PK] PersonDemographicsId int NOT NULL,
IdentificationNumber nvarchar(9) NOT NULL,
FirstName nvarchar(100) NOT NULL,
LastName nvarchar(100) NOT NULL
)
When we receive new data, the version number can change, but the rest of the demographics could be the same. What we need NHibernate to do is save a new record to the PersonVersion
table which links to the existing PersonDemographics
record. If the demographics have changed, then we will create a new record in both tables. But most of the time, once we've downloaded the initial data, the demographics won't change as often as the version number. We need to keep track of all version numbers so that's why it's necessary to create a new PersonVersion
record.
How would we accomplish this using NHibernate and mappings using Fluent NHibernate?
Also, as you can see, our Person object currently does not have a PersonDemographicsId
because it is not needed by our application at all; it is just an ID for the table relationship which is needed in the database. In order to properly map this in NHibernate, do we have to add a PersonDemographicsId
property on our Person object?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本文 http://ayende.com/blog/2327/multi-table -entities-in-nhibernate 解释了将单个类映射到数据库中的两个表的方法。
This article http://ayende.com/blog/2327/multi-table-entities-in-nhibernate explains a way to map a single class to two tables in the database.
只是一个想法,也许需要调整
just an idea, maybe has to be tweaked