问题
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