(流利的)Nhibernate Map 从单个 table 行收集对象,每个对象具有不同的列名 C#

(Fluent) Nhibernate Map collection of objects from single table row with different column names per object C#

我有一个客户 class,他有一个地址列表 class 作为 属性。我 运行 遇到的问题是当源是单个数据库 table.

中的单个记录时尝试使用 Fluent Nhibernate 映射地址 class

我的class如下:

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,
)

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

有谁知道如何正确执行此操作?

谢谢

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

但假设您不能这样做,您可以将地址 class 映射为一个组件,以分离 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
    }
}

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