2

我正在尝试使用 FNHib 自动映射来映射我的收藏。我要解决的问题是:

1)我希望我在项目中的所有收藏都通过私有字段进行映射。我怎么能在全球范围内这么说?

2)有没有办法在不明确覆盖我的每个实体的情况下自动映射双向关系。

类组织实体示例:

private ISet<> _collectionWarehouse;
public virtual IEnumerable<WarehouseEntity> CollectionWarehouse
{

get{return _collectionWarehouse; }

set{_collectionWarehouse = new HashedSet<WarehouseEntity>((ICollection<WarehouseEntity>)value)}

}

类 WarehouseEntity 示例:

public virtual OrganizationEntity Organization{get;set;}
4

1 回答 1

3

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;
于 2010-07-09T20:31:55.440 回答