25

Fluent NHibernate を使用して、残りのデータベース スキーマと共にテーブル インデックスを生成することは可能ですか? 自動化されたビルド プロセスを介して完全なデータベース DDL を生成できるようにしたいと考えています。

4

3 に答える 3

49

Fluent NHibernateの最近のバージョンでは、(現在は存在しない)Index()を使用する代わりに、メソッドを呼び出してこれを行うことができます。SetAttribute

Map(x => x.Prop1).Index("idx__Prop1");
于 2009-11-19T08:07:00.800 に答える
15

列のインデックスを意味しますか?

ClassMap<...>を追加することにより、ファイルで手動で行うことができます.SetAttribute("index", "nameOfMyIndex")。たとえば、次のようになります。

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

または、オートマッパーの属性機能を使用してそれを行うことができます-たとえば、次のように:

永続化モデルを作成した後:

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

私は後者が好きです。これは、ドメイン モデルに干渉しないことと、非常に効果的で何が起こっているのかを明確にすることとの間の適切な妥協点です。

于 2009-03-03T20:24:02.783 に答える
10

Mookid の回答は素晴らしく、私には大いに役立ちましたが、その間、進化を続ける Fluent NHibernate API が変更されました。

したがって、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] 属性は同じように機能します。

于 2009-05-26T12:52:21.397 に答える