component collection mapping NHibernate 3.2

被刻印的时光 ゝ 提交于 2020-01-06 03:32:24

问题


I have a Customer which has Addresses:

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; }
}

I want to map this to NHibernate 3.2. which has fluent interface, but not exactly the same as well-known Fluent NHibernate. I know that I have to use Bag, and maybe Element? Or component? So it should be something like this:

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

Thanks.


回答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"));
           });
    }
}

more info: http://moh-abed.com/2011/08/14/nhibernate-3-2-mapping-entities-and-value-objects-by-code/



来源:https://stackoverflow.com/questions/7190773/component-collection-mapping-nhibernate-3-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!