是否可以指定 Index 属性的名称以用于流畅的 nhibernate 约定中的列表?

发布于 2024-08-25 01:20:09 字数 474 浏览 2 评论 0原文

当在 fluent nhibernate 中映射 HasMany 或 HasManyToMany 时,您可以指定用于列表的列名作为 AsList() 方法的参数,如下所示:

HasMany(c => c.Customers)
    .AsList(c => c.Column("PositionIndex"));

我希望能够使用 Fluent NHibernate 约定(或者是预先存在的名称或自定义名称),特别是因为默认名称似乎是“Index”,它是 MSSQL 中的保留字。

我尝试使用实现 IHasManyConvention 的自定义约定,但实例参数似乎不包含有关其是列表、包还是集合的信息,也不包含索引列的列详细信息。

public void Apply(IOneToManyCollectionInstance instance)
{

}

有什么想法吗?

When mapping a HasMany or HasManyToMany in fluent nhibernate, you can specify the column name to use for the list as a parameter to the AsList() method as follows:

HasMany(c => c.Customers)
    .AsList(c => c.Column("PositionIndex"));

I would prefer to be able to set this using a Fluent NHibernate convention (either a pre-existing one, or a custom one), especially since the default name appears to be "Index" which is a reserved word in MSSQL.

I've tried using a custom convention implementing IHasManyConvention, but the instance parameter does not seem to contain the information about whether its a list, a bag, or a set, and also does not contain the column details for the index column.

public void Apply(IOneToManyCollectionInstance instance)
{

}

Any ideas?

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

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

发布评论

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

评论(2

山人契 2024-09-01 01:20:09

当应用约定时,底层映射已经生成。目前无法通过约定将此映射更改为有序集合(或任何其他类型)。

但是,您仍然可以通过 IAutoMappingOverride<> 更改集合的类型。因为它们是在公约之前应用的。

即使尚不支持此功能,它在下一个版本的待办事项列表中看起来也相当靠前。请参阅此 线程 了解更多详情。

When the convention is being applied, the underlying mapping has already been generated. There is currently no way to change this mapping to an ordered collection (or any other type) through convention.

However, you can still change the type of collection through an IAutoMappingOverride<> as they are applied prior to the conventions.

Even if this is not supported yet, it looks like quite high on the Todo list for the next release. See this thread for further details.

江南烟雨〆相思醉 2024-09-01 01:20:09

以防万一有人

从 FNH 1.2 开始来到这里,可以将包更改为在大会中列出。这样我就实现了:

class CollectionsAreListsConvention : ICollectionConvention
{
    public void Apply(ICollectionInstance instance)
    {
        instance.AsList();
        instance.Key.Column(instance.EntityType.Name + "_id");

        var mapping = (CollectionMapping)instance.GetType()
            .GetField("mapping", BindingFlags.Instance | BindingFlags.NonPublic)
            .GetValue(instance);

        if (!mapping.HasValue(m => m.Index))
        {
            var indexmapping = new IndexMapping();

            indexmapping.AddColumn(new ColumnMapping
            {
                // for Classes with more than one collection to another Class
                Name = instance.Member.Name + "_position",
            });

            mapping.Index = indexmapping;
        }
    }
}

虽然不完美,但对于我的项目来说已经足够了,有大量的列表

just in case someone comes here

since FNH 1.2 it is possible to change bag to list in a convention. With that i implemented:

class CollectionsAreListsConvention : ICollectionConvention
{
    public void Apply(ICollectionInstance instance)
    {
        instance.AsList();
        instance.Key.Column(instance.EntityType.Name + "_id");

        var mapping = (CollectionMapping)instance.GetType()
            .GetField("mapping", BindingFlags.Instance | BindingFlags.NonPublic)
            .GetValue(instance);

        if (!mapping.HasValue(m => m.Index))
        {
            var indexmapping = new IndexMapping();

            indexmapping.AddColumn(new ColumnMapping
            {
                // for Classes with more than one collection to another Class
                Name = instance.Member.Name + "_position",
            });

            mapping.Index = indexmapping;
        }
    }
}

Not perfect but enough for my project with tons of lists

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