私のコード Entity Framework では、ナビゲーション プロパティを 1 つだけ読み込みます
//Model
public class MemorialData
{
public MemorialData()
{
Id = Guid.NewGuid();
}
[Key]
public Guid Id { get; set; }
public virtual Memorial Memorial { get; set; }
public Guid MemorialId { get; set; }
public virtual UserData Author { get; set; }
public Guid AuthorId { get; set; }
public string Data { get; set; }
}
public ActionResult AddData(MemorialData model)
{
MemorialData md = db.MemorialDatas.Find(model.Id);
if (md == null)
{
System.Diagnostics.Debug.WriteLine("md == null Creating");
md = new MemorialData();
md.MemorialId = model.MemorialId;
//md.Memorial = db.Memorials.Find(model.MemorialId);
md.Data = model.Data;
md.AuthorId = db.CurrentUser.Id;
db.MemorialDatas.Add(md);
}
else
{
md.Data = model.Data;
}
System.Diagnostics.Debug.WriteLine("Saving " + md.Id);
db.SaveChanges();
System.Diagnostics.Debug.WriteLine("Load " + md.Id);
md = db.MemorialDatas.Find(md.Id);
System.Diagnostics.Debug.WriteLine("AuthorId " + md.AuthorId);
System.Diagnostics.Debug.WriteLine("Author is NULL? " + (md.Author == null));
System.Diagnostics.Debug.WriteLine("MemorialId " + md.MemorialId);
System.Diagnostics.Debug.WriteLine("Memorial is NULL? " + (md.Memorial == null));
}
デバッグ出力
md == null 作成中
保存中 1b21f49f-749d-4cd3-8c3f-1f128ce67f6f
1b21f49f-749d-4cd3-8c3f-1f128ce67f6f をロード
AuthorId 7b1fc9c4-4930-45be-9196-44b3f49e6770
著者は NULL ですか? 間違い
MemorialId a813a8f4-409b-4c97-8d2d-cb19aff267bb
メモリアルはNULL?真実 ???
なぜメモリアルは null で、Author は null ではないのですか?
エラーの原因はどこにありますか?
PS
If uncomment //md.Memorial = db.Memorials.Find(model.MemorialId);
メモリアルはNULL?間違い ???