UserProperties というテーブルがあります。ユーザーごとに少数のユーザー プロパティのセットがあります。それらをプログラムの_userPropertiesという Dictionary にロードします(キャッシュされるように)。
基本的に、テーブルは次のようになります。
列: CreatedOn、ModifiedOn、CreatedBy、および ModifiedBy は、厳密に目的をデバッグするためのものであるため、DAL (dbml) の一部ではありません。トリガーは ModifiedBy と ModifiedOn を設定します。
ユーザーが自分の設定を保存したい場合 (またはプログラムが設定を保存する必要があると判断した場合)、次のコードを呼び出します。
string userPropertyValueAsString = (String)Convert.ChangeType(userPropertyValue, typeof(String));
if (_userProperties.ContainsKey(userPropertyKey))
{
if (_userProperties[userPropertyKey] != userPropertyValueAsString)
{
using (DataAccessDataContext dataContext = CreateContext(JsApplication.CommitDal))
{
(1) UserProperty changedUserProperty = dataContext.UserProperties.First(u => u.fk_Employee == employeeId && u.PropertyName == userPropertyKey);
(2) changedUserProperty.PropertyValue = userPropertyValueAsString;
_userProperties[userPropertyKey] = changedUserProperty.PropertyValue;
if (!dataContext.SubmitChanges())
{
throw new SubmitChangesException(employeeId);
}
}
}
}
(1) に到達すると、クラス UserProperty のコンストラクターが呼び出されます (データベースのテーブルで UserProperty が検出されるため、予想どおり)。しかし、(2) に到達すると、コンストラクターが再度呼び出され、2 番目のインスタンスが作成され、困惑します。
例外はスローされず、余分なインスタンスがテーブルに保存されます (余分なインスタンスにはプロパティ値の変更が含まれており、次に古い値が小さい ID (古いもの) を持つ行を介してデータベースから読み取られるため、バグが発生します) )。
コールスタックは次のようになります (コンストラクターにブレークポイントを設定し、(2) の直後にスクリーンダムをキャプチャしました。
WPFがこれを行う理由と、それを停止する方法を教えてください。
Windows: Windows 7 Ultimate 64 ビット Visual Studio: Visual Studio 2010 Ultimate