使用Fluent NHibernate,如何将一个实体映射到不同命名的表?

时间:2012-11-07 16:59:29

标签: nhibernate-mapping fluent-nhibernate-mapping

要清楚,我希望将整个对象及其所有属性映射到基本相同的表的不同副本。我的搜索向我展示了如何在多个表中拆分对象的属性,但这不是我想要完成的。

这是我的对象模型(剥离):

class Customer 
{
   public Guid CustomerGuid { get; set; }
   public string Name { get; set; }
   public Address Address { get; set; }
}

class Address
{
   public Guid AddressGuid { get; set; }
   public string Line1 { get; set; }
   public string State { get; set; }
}

class Application
{
   public Guid ApplicationGuid { get; set; }
   public Address Address { get; set; }
   public DateTime SubmittedDate { get; set; }
}

问题是我需要将Address作为一个组件进行排序,但是要保存到两个单独的表中:CustomerAddress和ApplicationAddress,如下所示:

table Customer
(
   CustomerGuid
   Name
)

table Application
(
   ApplicationGuid
   SubmittedDate
)

table CustomerAddress
(
   CustomerGuid
   Line1
   State
)

table ApplicationAddress
(
   ApplicationGuid
   Line1
   State
)

我知道我可以使用一对一(HasOne)作为Customer到CustomerAddress来完成其中一个映射,但是如何使用Application to ApplicationAddress做同样的事情呢?

1 个答案:

答案 0 :(得分:1)

用于客户和模拟应用程序

class CustomerMap : ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x => x.Id, "CustomerGuid").GeneratedBy.GuidComb();

        Map(x => x.Name);
        Join("CustomerAddress", join =>
        {
            join.KeyColumn("CustomerGuid");
            join.Component(x => x.Address, c =>
            {
                c.Map(x => x.Line1);
                c.Map(x => x.State);
            });
        });
    }
}