组件集合映射NHibernate 3.2

时间:2011-08-25 12:58:48

标签: c# .net nhibernate

我的客户有地址:

public class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
   public ICollection<Address> Addresses { get; private set; }
}

public class Address 
{
   public string City { get; set; }
   public string Country { get; set; }
   public string StreetName { get; set; }
   public string ZipCode { get; set; }
}

我想把它映射到NHibernate 3.2。它具有流畅的界面,但与众所周知的Fluent NHibernate不完全相同。 我知道我必须使用Bag,也许是Element?还是组件? 所以它应该是这样的:

Bag( 
   property => property.Addresses,
   collectionMapping => {}, // not important now
   mapping => // and what should I use here, and how?
  );

感谢。

1 个答案:

答案 0 :(得分:1)

public class CustomerMap : ClassMapping<Customer>
{
    public CustomerMap()
    {
        Id(x => x.ID, map => map.Generator(Generators.HighLow,
                      gmap => gmap.Params(new {max_low = 100})));
        Property(x => x.Name,
                      map => { map.Length(150); map.NotNullable(true); });
        Bag(x => x.Addresses,
           collectionMapping =>
           {
               collectionMapping.Table("CustomerAddresses");
               collectionMapping.Cascade(Cascade.All);
               collectionMapping.Key(k => k.Column("CustomerId"));
           });
    }
}

更多信息:http://moh-abed.com/2011/08/14/nhibernate-3-2-mapping-entities-and-value-objects-by-code/