Generate table indexes using Fluent NHibernate

血红的双手。 提交于 2020-01-28 17:59:44

问题


Is it possible to generate table indexes along with the rest of the database schema with Fluent NHibernate? I would like to be able to generate the complete database DDL via an automated build process.


回答1:


In more recent versions of Fluent NHibernate, you can call the Index() method to do this rather than using SetAttribute (which no longer exists):

Map(x => x.Prop1).Index("idx__Prop1");



回答2:


Do you mean indexes on columns?

You can do it manually in your ClassMap<...> files by appending .SetAttribute("index", "nameOfMyIndex"), e.g. like so:

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

or you can do it by using the attribute features of the automapper - e.g. like so:

After having created your persistence model:

{
    var model = new AutoPersistenceModel
    {
        (...)
    }

    model.Conventions.ForAttribute<IndexedAttribute>(ApplyIndex);
}


void ApplyIndex(IndexedAttribute attr, IProperty info)
{
    info.SetAttribute("index", "idx__" + info.Property.Name");
}

and then do this to your entities:

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

I like the latter. Is is a good compromise between not being non-instrusive to your domain model, yet still being very effective and clear on what is happening.




回答3:


Mookid's answer is great and helped me a lot, but meanwhile the ever evolving Fluent NHibernate API has changed.

So, the right way to write mookid sample now is the following:

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

where IndexedPropertyConvention is the following:

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

The [Indexed] attribute works the same way now.



来源:https://stackoverflow.com/questions/607935/generate-table-indexes-using-fluent-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!