Fluent nhibernate 自动映射集合

发布于 2024-09-08 07:14:44 字数 569 浏览 9 评论 0原文

我正在尝试使用 FNHib 自动映射来映射我的集合。我想解决的问题是:

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

2)有没有什么方法可以自动映射双向关系,而无需显式覆盖我的每个实体。

类 OrganizationEntity 示例:

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;}

I am trying to map my collections with FNHib automapping. The problems that I want to solve are:

1) I want all my collections in the project to be mapped via private field. How can I say that globally?

2) Is there any way to automap bidirectional relationship without explicitly overriding each of my entities.

class OrganizationEntity example:

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

get{return _collectionWarehouse; }

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

}

Class WarehouseEntity example:

public virtual OrganizationEntity Organization{get;set;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2024-09-15 07:14:44

您可以使用以下约定将集合“全局”映射到私有字段:

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

每当您想在 FNH 中设置“全局”自动映射首选项时,请考虑约定。如果需要,您可以在给定的类映射上使用 IAutoOverride。

就集合(哈希集通常也是我真正想要的)部分而言,上次我必须做一些映射时,我确实需要进行覆盖,例如:

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

    }
}

我确实同意应该转化为约定,并且也许现在你可以做到这一点。如果你弄清楚了请发帖。

HTH,
Berryl代码 =================

使用 HASHSET 作为 ICollection 的

public virtual ICollection<WarehouseEntity> Wharehouses
{
    get { return _warehouses ?? (_warehouses = new HashSet<WarehouseEntity>()); }
    set { _warehouses = value; }
}
private ICollection<WarehouseEntity> _warehouses;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文