更新实体框架中的子表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更新实体框架中的子表相关的知识,希望对你有一定的参考价值。
我的数据库中有2个表:“竞争”和“竞争结果”。每个比赛将始终有3个答案。我希望能够为同一视图编辑Competition和CompetitionAnswers。我设法通过使用编辑器模板使视图起作用
这是我的编辑器模板:
@ModelType golfbug.Answer
<div class="editor-label left">
Answer:<br />
</div>
<div class="editor-field right">
@html.HiddenFor(Function(model) model.CompetitionID)
@Html.HiddenFor(Function(model) model.CompetitionAnswersID)
@Html.EditorFor(Function(model) model.CompetitionAnswer)
</div>
<div class="clear"></div>
这是我的观点
@ModelType golfbug.Competition
@Code
ViewData("Title") = "Edit"
End Code
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>Competition</legend>
@Html.HiddenFor(Function(model) model.CompetitionID)
<div class="editor-label left">
@Html.LabelFor(Function(model) model.ClientID, "Client")
</div>
<div class="editor-field right">
@Html.DropDownList("ClientID", String.Empty)
@Html.ValidationMessageFor(Function(model) model.ClientID)
</div>
<div class="clear"></div>
<div class="editor-label left">
@Html.LabelFor(Function(model) model.StartDate)
</div>
<div class="editor-field right">
@Html.EditorFor(Function(model) model.StartDate)
@Html.ValidationMessageFor(Function(model) model.StartDate)
</div>
<div class="clear"></div>
<div class="editor-label left">
@Html.LabelFor(Function(model) model.CompetitionName)
</div>
<div class="editor-field right">
@Html.EditorFor(Function(model) model.CompetitionName)
@Html.ValidationMessageFor(Function(model) model.CompetitionName)
</div>
<div class="clear"></div>
<div class="editor-label left">
@Html.LabelFor(Function(model) model.CompetitionDetails)
</div>
<div class="editor-field right">
@Html.EditorFor(Function(model) model.CompetitionDetails)
@Html.ValidationMessageFor(Function(model) model.CompetitionDetails)
</div>
<div class="clear"></div>
<div class="editor-label left">
@Html.LabelFor(Function(model) model.CompettionQuestion)
</div>
<div class="editor-field right">
@Html.EditorFor(Function(model) model.CompettionQuestion)
@Html.ValidationMessageFor(Function(model) model.CompettionQuestion)
</div>
<div class="clear"></div>
@Html.EditorFor(Function(model) model.Answers)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
End Using
<div>
@Html.ActionLink("Back to List", "Index")
</div>
当我点击保存按钮时,我需要控制器更新答案表中的答案。目前,我只有Visual Studio生成的代码。我想我必须遍历答案并更新答案,但是我不确定如何做到这一点。
这是我的控制器代码:
Function Edit(competition As Competition) As ActionResult
If ModelState.IsValid Then
db.Competitions.Attach(competition)
db.ObjectStateManager.ChangeObjectState(competition, EntityState.Modified)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.ClientID = New SelectList(db.Clients, "ClientID", "ClientName", competition.ClientID)
Return View(competition)
End Function
任何帮助将不胜感激。我已经在Google上搜索了2天了。
答案
[当您检查competition.Answers
时,您是否看到该属性中的值?如果是这样,则必须像这样手动添加它们:
db.Answers.Add(competition.Answers[0]);
也请查看此链接,并提供有关Attach
和Add
之间的区别的信息>
Entity Framework 4 - AddObject vs Attach
由于您说过comptetitions
总是有3个答案,所以这里有一个替代解决方案:当您创建
CompetitionAnswers
(带有将其标记为未回答的标志)。然后,您的编辑代码应该可以正常工作。以上是关于更新实体框架中的子表的主要内容,如果未能解决你的问题,请参考以下文章
父 - 事务下 PHP PDO SQLite 中的子表 - 没有错误,但它不起作用