最初に行を取得せずに、Linq to Entitiesを使用して、テーブルから1つ以上の行を削除するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1601350

  •  05-07-2019
  •  | 
  •  

質問

削除ストアドプロシージャを特定のタイプの削除メソッドにマップできることを理解しています。

ただし、これには、取得したオブジェクトをコンテキストの DeleteObject メソッドに渡す必要があります。

これで十分ですが、2000行を削除したい場合はどうすればよいですか?最初にデータベースからこれらの2000行を取得し、 DeleteObject を呼び出すループを実行せずに、Linq to Entitiesでこれを実行できますか?

このような機能がLinq to Entitiesに存在せず、これが事実であることがわかっている場合は、そう言ってください。他のオプションを調査します。

直接存在しない場合、ストアドプロシージャをLinq経由でエンティティにパイプすることで実現できますか?

役に立ちましたか?

解決

はい、これを行うことができます。 このヒント

// Create an entity to represent the Entity you wish to delete
// Notice you don't need to know all the properties, in this
// case just the ID will do.
Category stub = new Category { ID = 4 };
// Now attach the category stub object to the "Categories" set.
// This puts the Entity into the context in the unchanged state,
// This is same state it would have had if you made the query
ctx.AttachTo("Categories", stub);
// Do the delete the category
ctx.DeleteObject(stub);
// Apply the delete to the database
ctx.SaveChanges();

ヒントと詳細については、完全なヒントを参照してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top