私たちは、作成中の MVC4 アプリに対して EF5 (Code First アプローチ) を使用しています。エンティティの 1 つのプロパティを更新しようとしていますが、エラーが発生し続けます。コンテキストが作成したクラスは次のようになります。
public partial class Room
{
public Room()
{
this.Address = new HashSet<Address>();
}
public int RoomID { get; set; }
public Nullable<int> AddressID { get; set; }
public Nullable<int> ProductVersionID { get; set; }
public string PhoneNumber { get; set; }
public string AltPhone { get; set; }
public string RoomName { get; set; }
public string Description { get; set; }
public string Comments { get; set; }
public string Notes { get; set; }
public virtual ICollection<Address> Address { get; set; }
}
ビューの ViewModel は次のとおりです。
public class RoomDetailsViewModel
{
//public int RoomID { get; set; }
public string RoomName { get; set; }
public string PhoneNumber { get; set; }
public string AltPhone { get; set; }
public string Notes { get; set; }
public string StateCode { get; set; }
public string CountryName { get; set; }
public string ProductVersion { get; set; }
public int PVersionID { get; set; }
public List<SelectListItem> ProductVersions { get; set; }
public Room Room { get; set; }
}
「保存」で呼び出されるコントローラーアクションは次のとおりです。
[HttpPost]
public virtual ActionResult UpdateRoom(RoomDetailsViewModel model)
{
var db = new DBContext();
bool b = ModelState.IsValid;
var rooms = db.Rooms;
var rm = rooms.Where(r => r.RoomID == model.Room.RoomID).Single();
//List<Address> address = db.Addresses.Where(a => a.AddressID == rm.AddressID).ToList<Address>();
rm.ProductVersionID = model.PVersionID;
//rm.Address = address;
db.Entry(rm).Property(r => r.ProductVersionID).IsModified = true;
//db.Entry(rm).State = System.Data.EntityState.Modified;
db.SaveChanges();
return View("RoomSaved", model);
}
このビューはデータを表示し、ユーザーが (SelectList から) 製品バージョンを変更できるようにするだけなので、Room エンティティで更新しているのは ProductVersionID プロパティだけです。データを正しく表示することはできますが、[保存] をクリックすると次のエラーが発生します。
タイプ 'System.Collections.Generic.List`1[[Models.Address, Web.Mobile.TestSite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' のオブジェクトを値から設定または削除することはできませんタイプ「Models.Address」の EntityReference のプロパティ。
コントローラー アクションでわかるように、いくつかの異なることを試しましたが、すべてこのエラーが発生するようです。model.Room.Address コレクションにアドレスを入力しようとしましたが、アドレスを入力しませんでしたが、それでもこのエラーが発生します。
この StackOverflow の記事とこの記事も読みましたが、どちらも問題を解決していません。
これに関する任意の助けをいただければ幸いです。