【问题标题】:Is it possible to specify the name of the Index property to use for lists in a fluent nhibernate convention?是否可以指定索引属性的名称以用于流畅的 nhibernate 约定中的列表?
【发布时间】:2011-08-25 15:56:29
【问题描述】:

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

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

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

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

public void Apply(IOneToManyCollectionInstance instance)
{

}

有什么想法吗?

【问题讨论】:

    标签: fluent-nhibernate


    【解决方案1】:

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

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

    即使目前尚不支持此功能,它在下一个版本的待办事项列表中看起来也相当高。有关详细信息,请参阅此thread

    【讨论】:

    • 感谢您的回答,但是该线程讨论了使用约定来指定 IList 应该映射为列表而不是包。除非我遗漏了什么,否则它没有提及为索引列的列名指定约定
    • @Teevus:我不够清楚。对此感到抱歉。我已经相应地更新了答案。
    【解决方案2】:

    以防有人来这里

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2013-01-30
      • 2019-09-27
      • 2011-04-04
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多