代码之家  ›  专栏  ›  技术社区  ›  Todd Brooks

使用Fluent NHibernate生成表索引

  •  25
  • Todd Brooks  · 技术社区  · 17 年前

    3 回复  |  直到 17 年前
        1
  •  48
  •   Marijn    14 年前

    Index() 方法来执行此操作,而不是使用 SetAttribute (已不存在):

    Map(x => x.Prop1).Index("idx__Prop1");
    
        2
  •  15
  •   mookid8000    17 年前

    你可以在你的电脑里手动操作 ClassMap<...> 通过附加 .SetAttribute("index", "nameOfMyIndex") ,例如:

    Map(c => c.FirstName).SetAttribute("index", "idx__firstname");
    

    或者,您可以使用automapper的属性功能执行此操作,例如:

    创建持久化模型后:

    {
        var model = new AutoPersistenceModel
        {
            (...)
        }
    
        model.Conventions.ForAttribute<IndexedAttribute>(ApplyIndex);
    }
    
    
    void ApplyIndex(IndexedAttribute attr, IProperty info)
    {
        info.SetAttribute("index", "idx__" + info.Property.Name");
    }
    

    然后对实体执行以下操作:

    [Indexed]
    public virtual string FirstName { get; set; }
    

    我喜欢后者。Is是一个很好的折衷方案,既不妨碍您的领域模型,又能非常有效和清楚地了解正在发生的事情。

        3
  •  10
  •   Yann Schwartz    17 年前

    因此,现在编写mookid示例的正确方法如下:

    //...
    model.ConventionDiscovery.Setup(s =>
                {
                    s.Add<IndexedPropertyConvention>();
                    //other conventions to add...
                });
    

    其中IndexedPropertyConvention如下所示:

    public class IndexedPropertyConvention : AttributePropertyConvention<IndexedAttribute>  
    {
        protected override void Apply(IndexedAttribute attribute, IProperty target)
        {
             target.SetAttribute("index", "idx__" + target.Property.Name);
        }
    }
    

    [Indexed]属性现在的工作方式与此相同。

    推荐文章