MVC 3アプリケーションでコードファーストでEntity Framework 4.3を使用しています。私はエンティティをそのパラメータとして取得し、データベースを更新するように変更されたエンティティをマークするPOSTアクションを持っています。これは、File Typeへの参照を持つDocumentエンティティです。EF4.3 POSTでのアタッチ後のコードファースト、MVC、レイジーローディング
[HttpPost]
public ActionResult Example(Document model)
{
// fileType is null, as expected
var fileType = model.FileType;
// attach and mark the entity as modified, save changes
Context.Entry(model).State = EntityState.Modified;
Context.SaveChanges();
// fileType is still null?
fileType = model.FileType;
return View(model);
}
エンティティをコンテキストに添付した後で、そのエンティティのプロパティを遅延ロードできないようにしてください。
興味深いことに、これをコンソールアプリケーションで試してみるとうまくいくようです。
static void Main()
{
// create a new context
var context = new Context();
// get the first document and detach it
var doc = context.Documents.First();
context.Entry(doc).State = EntityState.Detached;
// fileType is null, as expected
var fileType = doc.FileType;
// dispose and create a new context
context.Dispose();
context = new Context();
// attach the entity and mark it as modified
context.Entry(doc).State = EntityState.Modified;
// fileType is not null, which is the desired outcome
fileType = doc.FileType;
}
ありがとうございます!あなたの説明を使用して、私はプロキシを生成するモデルバインダーを実装するための簡単なソリューションを見つけることができました。ここに私を助けた記事へのリンクがあります。 http://bit.ly/ydCjOm – user326502
@ user326502リンクをありがとう。スーパー便利 –