(流畅)Nhibernate Map单个表行中的对象集合,每个对象具有不同的列名C#

时间:2015-08-19 22:11:12

标签: c# sql-server nhibernate fluent-nhibernate

我有一个Customer类,其中包含一个Address类列表作为属性。我遇到的问题是当源是单个数据库表中的单个记录时,尝试使用Fluent Nhibernate映射Address类。

我的课程如下:

public class Customer
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Name { get; set; }
    public virtual string ContactName { get; set; }
    public virtual List<Address> Addresses { get; set; }
}

public class Address
{
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string Country { get; set; }
    public virtual string PostalCode { get; set; }
    public virtual AddressType Type { get; set; }
}

数据库架构如下:

TABLE [dbo].[CUSTOMER](
    [CUSTID] [int] NOT NULL,
    [NAME] [nvarchar](100) NULL,
    [FIRSTNAME] [nvarchar](100) NULL,
    [LASTNAME] [nvarchar](100) NULL,
    [CONTACTNAME] [nvarchar](100) NULL,
    [MAINADDRESS1] [nvarchar](100) NULL,
    [MAINADDRESS2] [nvarchar](100) NULL,
    [MAINCITY] [nvarchar](100) NULL,
    [MAINSTATE] [nvarchar](20) NULL,
    [MAINZIP] [nvarchar](100) NULL,
    [MAINCOUNTRY] [nvarchar](100) NULL,
    [SHIPADDRESS1] [nvarchar](100) NULL,
    [SHIPADDRESS2] [nvarchar](100) NULL,
    [SHIPCITY] [nvarchar](100) NULL,
    [SHIPSTATE] [nvarchar](20) NULL,
    [SHIPZIP] [nvarchar](40) NULL,
    [SHIPCOUNTRY] [nvarchar](100) NULL,
    [EnglishAddress1] [nvarchar](100) NULL,
    [EnglishAddress2] [nvarchar](100) NULL,
    [EnglishCity] [nvarchar](100) NULL,
    [EnglishState] [nvarchar](20) NULL,
    [EnglishZip] [nvarchar](40) NULL,
    [EnglishCountry] [nvarchar](50) NULL,
)

我想将单个客户记录中的每个地址映射到列表中具有不同地址类型的单个地址类。

有谁知道如何正确地做到这一点?

谢谢

1 个答案:

答案 0 :(得分:0)

嗯,最好的解决方案是重构您的架构,以便您有一个单独的地址表。

但假设您无法做到这一点,您可以将Address类映射为分离MainAddress,ShipAddress和EnglishAddress属性的组件,然后通过列表将它们公开以进行迭代。

public class Customer
{
    // other properties

    protected virtual Address MainAddress { get; set; }
    protected virtual Address ShipAddress { get; set; }
    protected virtual Address EnglishAddress { get; set; }

    private IEnumerable<Address> _addresses = null;
    public virtual IEnumerable<Address> Addresses
    { 
        get 
        {
            if (_addresses == null)
            {
                MainAddress.AddressType = AddressType.Main;
                ShipAddress.AddressType = AddressType.Ship;                                     
                EnglishAddress.AddressType = AddressType.English;
               _addresses = new[] { MainAddress, ShipAddress, EnglishAddress };
            }
            return _addresses;
        }
    }
}

然后映射如下

public class CustomerMap : ClassMap<Customer>
{
     public CustomerMap()
     {
         // map customer properties

         Component(Reveal.Member<Customer>("MainAddress"), m =>
         {
             m.Map(x => x.Address1, "MainAddress1");
             m.Map(x => x.Address2, "MainAddress2");
             // other address properties
         };

         Component(Reveal.Member<Customer>("ShipAddress"), m =>
         {
             m.Map(x => x.Address1, "ShipAddress1");
             m.Map(x => x.Address2, "ShipAddress2");
             // other address properties
         };

         // same for english address
    }
}

您无法添加其他地址,但无论如何,您无法真正将其添加到您的架构中。