Fluent nhibernate automapping collection

巧了我就是萌 提交于 2019-12-04 15:30:17

You can map your collections to a private field 'globally' with the following convention:

// assumes camel case underscore field (i.e., _mySet)
public class CollectionAccessConvention : ICollectionConvention
{
    public void Apply(ICollectionInstance instance) {
        instance.Access.CamelCaseField(CamelCasePrefix.Underscore);
    }
}

Whenever you want to set a 'global' automap preference in FNH, think conventions. The you use the IAutoOverride on a given class map if you need to.

As far has the set (a HashSet is usually what I really want also) part, the last time I had to do some mapping, I did need to do an override, like:

   public class ActivityBaseMap : IAutoMappingOverride<ActivityBase>
{
    public void Override(AutoMapping<ActivityBase> m)
    {
        ...
        m.HasMany(x => x.Allocations).AsSet().Inverse();

    }
}

I do agree that should translate into a convention though, and maybe you can do that these days. Please post if you figure it out.

HTH,
Berryl

CODE TO USE A HASHSET as an ICollection =================

public virtual ICollection<WarehouseEntity> Wharehouses
{
    get { return _warehouses ?? (_warehouses = new HashSet<WarehouseEntity>()); }
    set { _warehouses = value; }
}
private ICollection<WarehouseEntity> _warehouses;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!