Fluent NHibernateを使用してテーブルインデックスを生成する
-
03-07-2019 - |
質問
Fluent NHibernateを使用して、データベーススキーマの残りの部分とともにテーブルインデックスを生成することは可能ですか?自動化されたビルドプロセスを介して完全なデータベースDDLを生成できるようにしたいと思います。
解決
Fluent NHibernateの最新バージョンでは、Index()
(現在は存在しません)を使用する代わりに、SetAttribute
メソッドを呼び出してこれを行うことができます。
Map(x => x.Prop1).Index("idx__Prop1");
他のヒント
列のインデックスを意味しますか?
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; }
私は後者が好きです。ドメインモデルに邪魔にならないようにしながら、何が起こっているかについて非常に効果的で明確であるということの間の良い妥協点です。
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]属性は現在、同じように機能します。
所属していません StackOverflow