简体   繁体   English

流利的Nhibernate-映射层次结构

[英]Fluent Nhibernate - Mapping hierarchy

Im using nhibernate on my project and I have a table of soldiers: 我在项目中使用了nhibernate,并且有一张士兵表:

ID, FIRSTNAME, LASTNAME, COMMANDERID

So every soldier has a commande whicn is also a soldier. 所以每个拥有突击队的士兵也是一名士兵。

I have the class Soldier: 我有士兵课:

    public class Soldier
    {
        public virtual int Id { get; set;}
        public virtual Soldier Commander { get; set; }
        public virtual IList<Soldier> Soldiers { get; set; }
    }

My question is, how do I map the Soldiers property? 我的问题是,如何映射“士兵”财产? I tried the following : 我尝试了以下方法:

HasMany(x => x.Soldiers).KeyColumn("COMMANDERID");

But im getting an exception. 但即时通讯得到一个例外。

What you have here is a one-to-many relationship with the class itself (instead of another class). 您在这里拥有的是与类本身(而不是另一个类)的一对多关系。 So I believe you must have both "Reference" and "HasMany" specified in your mapping in the class Soldier. 因此,我相信您必须在“士兵”类的映射中同时指定“参考”和“ HasMany”。 Thus, I think the correct map for the class Soldier would be something like this: 因此,我认为士兵类的正确地图应该是这样的:

Table("Soldier");
Id(x => x.ID).GeneratedBy.Identity();
Map(x => x.FIRSTNAME);
Map(x => x.LASTNAME);
Reference(x => x.Commander).Column("COMMANDERID"); //Parent
HasMany(x => x.Soldiers).Cascade.All().Inverse().KeyColumn("COMMANDERID"); //Children

Not really sure about how Inverse and Cascade will work, once we are talking about the same entity. 一旦我们讨论同一个实体,就不太确定Inverse和Cascade将如何工作。 You may probably need to test it. 您可能需要对其进行测试。

This other question may also help you to solve your problem (the scenario in very similar): Fluent / NHibernate Collections of the same class 这个其他问题也可以帮助您解决问题(非常相似的场景): 相同类别的Fluent / NHibernate集合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM