Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 2.43 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. NHibernate 3.2 mapping choices, relations and POV?
  2. public class Category : Entity
  3.     {
  4.         public Category()
  5.         {
  6.             Products = new List<Product>();
  7.             SubCategories = new List<Category>();
  8.         }
  9.  
  10.         public virtual string Name { get; set; }
  11.         public virtual string Description { get; set; }
  12.         public virtual Category Parent { get; set; }
  13.         public virtual IEnumerable<Category> SubCategories { get; set; }
  14.         public virtual IList<Product> Products { get; set; }
  15.     }
  16.        
  17. class CategoryMap : ClassMapping<Category>
  18.     {
  19.         public CategoryMap()
  20.         {
  21.             // **************************************************
  22.             // Mapping of Id here will take precedence over the
  23.             // global conventions configured in the ModelMapper.
  24.             // **************************************************
  25.             //Id(x => x.Id, map =>
  26.             //{
  27.             //    map.Column("Id");
  28.             //    map.Generator(Generators.GuidComb);
  29.             //});
  30.  
  31.             Property(x => x.Name, m => m.Length(450));
  32.             Property(x => x.Description, m => m.Length(2000));
  33.  
  34.             Set(x => x.SubCategories, set =>
  35.                                           {
  36.                                               set.Key(k => k.Column("ParentCategoryId"));
  37.                                               set.Inverse(true);
  38.                                           } ,
  39.                                        ce => ce.OneToMany());
  40.  
  41.             ManyToOne(x => x.Parent, manyToOne =>
  42.                                          {
  43.                                              manyToOne.Column("ParentCategoryId");
  44.                                              manyToOne.Lazy(LazyRelation.NoLazy);
  45.                                              manyToOne.NotNullable(false);
  46.                                          });
  47.  
  48.  
  49.             Set(x => x.Products, set =>
  50.                                     {
  51.                                         set.Key(key =>
  52.                                         {
  53.                                             key.Column("ProductId");
  54.                                             key.ForeignKey("FK_Product_Category_ProductId");
  55.                                         });
  56.                                         set.Table("Product_Category");
  57.                                     },
  58.                                     ce => ce.ManyToMany(m => m.Column("CategoryId")));
  59.         }
  60.     }