NHibernate 3.2+的 Map by Code 的文档真的很少,除了Fabio Maulo的2篇blog(顺带说李永京的翻译,没任何新的研究).
让人感慨NH的没落, 也许从没有流行过.
在没有文档的时候尝试map by code, 非常痛苦.
当然像这种 this.Property(p => p.StartDate); 没涉及关系的很简单.
实战上的东西就不一样了.
public class TourInstance: Entity { public virtual Tour Tour { get; set; } public virtual IList<City> FromCity { get; set; } //出发城市 public virtual IList<City> BackCity { get; set; } //返回城市 public virtual DateTime StartDate { get; set; } public virtual DateTime EndDate { get; set; } public virtual int Capacity { get; set; } }
对应的Mapping 我是花了半天时间才摸索出来, 这样的效率怎么敢用来做项目,只能学习之用.
public class TourInstanceMapping : EntityMapping<TourInstance> { public TourInstanceMapping() { this.ManyToOne(x => x.Tour, m => { m.Column("TourID"); m.Cascade(Cascade.All); m.NotNullable(true); }); this.IdBag(x => x.FromCity, c => { c.Id(i=> { i.Column("ID"); i.Generator(Generators.Native); }); c.Key(k => { k.Column("TourInsID"); k.ForeignKey("FK_FromCity_TourInstance"); }); }, r => r.ManyToMany(m => { m.Column("CityID"); m.ForeignKey("FK_FromCity_City"); }) ); this.Property(p => p.StartDate); this.Property(p => p.EndDate); this.Property(p => p.Capacity); } }