1

したがって、オブジェクトのリストを更新する必要があるとしましょう。

      using(db)
      {
          repository = new Repository<Publication>(db);
          foreach (KeyValuePair<int,int> item in publications)
          {
                Publication publication = repository.GetById(item.Key);
                if (publication != null)
                {
                    publication.Quantity = publication.Quantity - item.Value;
                    if (publication.Quantity > 0)
                        db.Publication.Attach(publication);
                }
          }
          try
          {
              db.SaveChanges();
          }
          catch (DbUpdateConcurrencyException e)
          {
              throw new Exception("Could not update the database", e);
          }
        }
    }

すべてのオブジェクトを保存しようとしたときに、誰かが失敗した場合、それはブロック内にあるはずですcatchが、私の質問は、例外をスローする特定のオブジェクトを取得するにはどうすればよいですか?

4

1 に答える 1

3

あなたは、次のように文書化されたプロパティDbUpdateConcurrencyExceptionを持つをキャッチしています:Entries

データベースに保存できなかったエンティティを表すDbEntityEntryオブジェクトを取得します。

だから基本的にそれはあなたにすべての問題のあるものを与えます。

于 2013-01-17T21:03:06.147 に答える