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

让人想犯罪 __ 提交于 2019-12-25 16:19:10

问题


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). 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. 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?

Thanks!


回答1:


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. I used to have a convention taht set Inverse() on all collections and override its key column to parent_id



来源:https://stackoverflow.com/questions/27973003/fluent-nhibernate-with-automapping-how-to-get-a-parent-or-containg-object-fro

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