2009-03-03 19 views

答えて

44

流暢NHibernateはの最近のバージョンでは、むしろ(もはや存在しない)SetAttributeを使用するよりも、これを行うためにIndex()メソッドを呼び出すことができます。

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

正しいですが、APIが変更されました。これは現在、インデックスを設定する正しい方法と考えられています。私はこの変更を反映するためにこの質問の答えを更新しました。ありがとうございました。 –

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

私は後者が好きです。あなたのドメインモデルに悪影響を与えないようにすることと、何が起こっているかについては依然として非常に効果的で明確であることの間には良い妥協点ですか?

+0

を私が探していた正確に何が。ありがとうございました。 –

10

Mookidの答えはすばらしく、私を大いに助けましたが、一方で進化しているFluent NHibernate APIは変更されました。

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

[索引]属性は、今と同じように動作します:IndexedPropertyConventionは以下の通りです

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

だから、今mookidサンプルを書き込むための正しい方法は次のとおりです。

関連する問題