読み取り専用の DB フィールドである必要があるプロパティ (Fluent NHibernate Automapping)
-
13-12-2019 - |
質問
Fluent NHibernate と自動マッピング機能を併用しています。今、エンティティプロパティを「ReadOnlyFromDb」として宣言するために、構成、設定、カスタム属性など、何でも探しています。
MsSql データベースでは、特定のデータ行の他の値に応じて値が計算されるテーブルの 1 つで計算列を使用しています。ここで、エンティティ クラスでこの列を次のように宣言しました。
public virtual int STATUS { get; private set; }
テーブルの特定のデータを取得すると、すべてが正常に機能します。物件 STATUS
データテーブル内の特定の値が正しく入力されています。
しようとすると問題が発生します SaveUpdate()
特定のオブジェクト。いつも例外が発生します
A computed column cannot be the target of an INSERT or UPDATE statement
私の理解ではどちらが正しいですか - そしてそれがどのようにあるべきだったかです ;)!
基本的に構成、設定、カスタム属性など何でもいいので探しています。
Hey Fluent NHibernate - 特定のプロパティを取得する
propertyName
ただし、プロパティを挿入/更新しないでくださいpropertyName
そんなことありますか?それともこの場合の回避策はありますか?Fluent nhibernate wiki を検索しましたが、類似したケースは見つかりませんでした。
誰かがすでにこの問題に直面して解決していることを願っています。
これは、セッションを作成する方法のコードスニペットです(役立つかもしれません)。
public static ISessionFactory GetNHibernateSession()
{
if (nhibernateSession != null)
return nhibernateSession;
if (ConfigurationManager.AppSettings[msSqlConnectionString] == null || ConfigurationManager.AppSettings[msSqlConnectionString] == String.Empty)
throw new NullReferenceException(String.Format("AppSetting '{0}' must not be null!", msSqlConnectionString));
//nhibernateSession = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager.AppSettings[msSqlConnectionString]))
// .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<T_ABWEICHUNGEN>().IgnoreBase<BaseClass>)).BuildSessionFactory();
nhibernateSession = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager.AppSettings[msSqlConnectionString]))
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(System.Reflection.Assembly.GetExecutingAssembly())
.Override<T_ABWEICHUNGEN>(map =>map.Map(d => d.A08_STATUS_FILE).Length(2147483647))
.IgnoreBase(typeof(BaseClass))
.IgnoreBase(typeof(IDColumn))
.IgnoreBase(typeof(MsSqlQuery))
.IgnoreBase(typeof(MsSqlParamCollection))
.IgnoreBase(typeof(AbweichungStatus))
)).BuildSessionFactory();
return nhibernateSession;
}
}
これまでの返答に感謝します。これまで私を助けてくれました。しかし、これをより「動的に」解決する方法はあるのでしょうか?
例えば:
というカスタム属性を宣言したい [ReadOnlyDbField]
次に、このカスタム属性を使用してエンティティのすべてのプロパティを宣言します。この値を読み取るだけで、更新/挿入は行わないでください。
基本的に構成で言いたいのは
Map all properties with the custom attribute [ReadOnlyDbField] to Not.Insert().Not.Update()
これを実装する方法はありますか?
解決
.Override<Entity>(map => map.Map(d => d.STATUS).Not.Insert().Not.Update())
アップデート:編集された質問のアカウント
public class ReadonlyDbFielConvention : AttributePropertyConvention<ReadOnlyDbField>
{
protected override void Apply(ReadOnlyDbField attribute, IPropertyInstance instance)
{
instance.Not.Insert();
instance.Not.Update();
}
}