简体   繁体   English

流利的具有自动映射功能的nHibernate:如何从子级获取父级或“污染”对象

[英]Fluent nHibernate with Automapping: How to get a parent or “containg” object from a child

I have an object model similar to this: 我有一个与此类似的对象模型:

    public class Item
    {
        public virtual Guid Id { get; set; }
    }

    public class Box
    {
        public virtual Guid Id { get; set; }
        public virtual IList<Item> Contents { get; protected set; }

        public Box()
        {
            Contents = new List<Item>();
        }
    }

What I want to do is be able to get an Item's parent (being the box it is in). 我想做的就是能够获得Item的父项(即它所在的框)。 Normally I would create a field on the Item called Parent and handle the one-to-many relationship in a manual mapping, but we are trying to use automapping on this project. 通常,我会在Item上创建一个名为Parent的字段,并在手动映射中处理一对多关系,但是我们试图在此项目上使用自动映射。 Is there someway I can create either a Box or a BoxId field ID on the Item and have FNH auto-maigially populate for me when object (in this case Box) is saved? 是否有办法在项目上创建Box或BoxId字段ID,并在保存对象(在这种情况下为Box)的情况下自动为我自动填充FNH?

Thanks! 谢谢!

in the object model der is no difference between manual mapping and automapping 在对象模型中,手动映射和自动映射没有区别

public class Box
{
    public virtual Guid Id { get; set; }
    public virtual IList<Item> Contents { get; protected set; }

    public Box()
    {
        Contents = new List<Item>();
    }

    public void Add(Item item)
    {
        item.Parent = this;
        Contents.Add(item);
    }
}

In automapping you have to make sure the Contents collection is marked inverse either through override or convention and that its keycolumn is the same as the Parent property column. 在自动映射中,必须确保通过重写或约定将Contents集合标记为反向,并且其键列与Parent属性列相同。 I used to have a convention taht set Inverse() on all collections and override its key column to parent_id 我曾经有过一个约定taht设定Inverse()在所有集合并覆盖其键列parent_id

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

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