我使用VS2015社區版MVC5之後從實體框架的所有數據恢復 - 我EF6如何更新模型
創建了一個簡單的模型及其CRUD操作。模型:Person,Fields(Id,FirstName,LastName,DateofBirth ...)。
當我更新模型(添加兩個引用字段與默認值)。
添加到人物模型的字段:(國籍_Id,城市_Id),他們每個人都是國籍和城市表格的外鍵。
當我返回到索引方法
// GET:
public ActionResult Index()
{
List<Person> persons = db.Persons.ToList();
return View(persons);
}
public class Person
{
public int Id { get; set; }
public string DocumentCode { get; set; }
public string Name1 { get; set; }
public string Name2 { get; set; }
public string Name3 { get; set; }
public string Name4 { get; set; }
public DateTime DateOfBirth { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Name { get; set; }
public List<Person> persons { get; set; }
}
下面是一些控制器方法
// GET: People
public ActionResult Index()
{
List<Person> persons = db.Persons.ToList();
return View(persons);
}
// GET: People/Create
public ActionResult Create()
{
ViewBag.Addresses = new SelectList(db.Adresses, "Id", "Name");
return View();
}
// POST: People/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,DocumentCode,Name1,Name2,Name3,Name4,DateOfBirth,Address")] Person person)
{
if (ModelState.IsValid)
{
db.Persons.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
這裏是一段代碼中創建視圖(應負責從視圖中檢索數據)
<div class="form-group">
@Html.LabelFor(model => model.Address, ClinicDemo.App_Start.SystemTranslation.ADDRESS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Address_Id", (IEnumerable<SelectListItem>)ViewBag.Addresses, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
這是一段代碼,應根據廣告查看地址名稱dress_id,它仍然是空的,即使在一些硬編碼數據
<td>
@Html.DisplayFor(modelItem => item.Address.Name)
</td>
人反對恢復所有數據,甚至沒有當我從數據庫中手動設置,當我添加新的人的所有數據都設置正確,除了那些字段中的新領域,
任何幫助?
請顯示新的Person類和代碼,在其中創建一個新的Person。沒有它就很難猜出有什麼不對。 –