2

エンティティではなく、エンティティのプロパティについてのみ状態を取得するにはどうすればよいですか?

製品クラスのオブジェクトがあり、その価格値のみが変更されたとしましょう。

  1. ProductName Unchanged
  2. ProductPriceが変更されました

オブジェクトが変更されたことは簡単にわかりますが、変更された正確なプロパティを知りたいです。

どうすればこれを行うことができますか?

4

2 に答える 2

2

ObjectStateEntryを使用して、このようにGetModifiedPropertiesメソッドを呼び出します(これには必要以上のサンプルがありますが、以下のコードでGetModifiedPropertiesを参照してください)。

int orderId = 43680;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    var order = (from o in context.SalesOrderHeaders
                 where o.SalesOrderID == orderId
                 select o).First();

    // Get ObjectStateEntry from EntityKey.
    ObjectStateEntry stateEntry =
        context.ObjectStateManager
        .GetObjectStateEntry(((IEntityWithKey)order).EntityKey);

    //Get the current value of SalesOrderHeader.PurchaseOrderNumber.
    CurrentValueRecord rec1 = stateEntry.CurrentValues;
    string oldPurchaseOrderNumber =
        (string)rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber"));

    //Change the value.
    order.PurchaseOrderNumber = "12345";
    string newPurchaseOrderNumber =
        (string)rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber"));

    // Get the modified properties.
    IEnumerable<string> modifiedFields = stateEntry.GetModifiedProperties();
    foreach (string s in modifiedFields)
        Console.WriteLine("Modified field name: {0}\n Old Value: {1}\n New Value: {2}",
            s, oldPurchaseOrderNumber, newPurchaseOrderNumber);

    // Get the Entity that is associated with this ObjectStateEntry.
    SalesOrderHeader associatedEnity = (SalesOrderHeader)stateEntry.Entity;
    Console.WriteLine("Associated Enity's ID: {0}", associatedEnity.SalesOrderID);
}
于 2012-06-27T01:39:13.280 に答える
1

インターウェブを見てみると、あなたがやりたいことを正確に説明しているこの記事を見つけました。

于 2012-06-26T20:56:53.347 に答える