【问题标题】:Entity Framework 4: Saving one to many entities doesn't work实体框架 4:保存一对多实体不起作用
【发布时间】:2010-10-31 21:31:50
【问题描述】:

这是一个常见的场景
我有这些实体
用户
用户ID
用户名
...

用户问题
用户QID
用户ID
用户问题
用户回答


一个用户可以有很多问题,但一个问题附加到一个用户。
在我的示例中,我创建了 3 个空实体 (UserQuestion)。然后我将这些问题附在用户身上。
在视图中,对于每个问题、答案文本框,我使用 ElementAt 来指定每个 UserQuestion Entity。
为什么我不能保存这三个问题


在控制器中

public ActionResult ChooseQuestion()
{
    IUserRepository repUser = new UserRepository(EntityFactory.GetEntity());
    User usr = repUser.GetUserByID(Convert.ToInt32(Session["UserID"]));
    usr.UserQuestions.Add(new UserQuestion());
    usr.UserQuestions.Add(new UserQuestion());
    usr.UserQuestions.Add(new UserQuestion());
    var ViewModel = new UsrViewModel()
                    {
                        UserInfo = usr
                    };   
    return View(ViewModel);

}

[HttpPost]
public void ChooseQuestion(User UserInfo)
{
    UpdateModel(User, "UserInfo");
    EntityFactory.GetEntity().SaveChanges();
}

在我看来

<%: Html.TextBoxFor(model => model.UserInfo.UserName)%>
...
<%: Html.TextBoxFor(model => model.UserInfo.LastName%>
...
<%: Html.TextBoxFor(model => model.UserInfo.UserPassword%>
..
<h2>Question Creation</h2>
<%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserQuestion)%>
<%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserAnswer)%>

<%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(1).UserQuestion)%>
<%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(1).UserAnswer)%>

<%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(2).UserQuestion)%>
<%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(2).UserAnswer)%>

3

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-2 entity-framework-4 entity-relationship


    【解决方案1】:

    基于Phil Haack's post,您的名称属性中应该只包含序列号,如下所示:

    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserQuestion, new {name="UserInfo.UserQuestion0.UserQuestion} )%>
    

    通常,如果内置 HtmlHelper.TextboxFor 方法在 for 循环中,它会正确执行 name 属性。

    由于我现在不在我的 VS 附近,所以我不能自己尝试这个魔法,但如果你遇到困难,你可能想查看 Phil 的文章。

    【讨论】:

    • 感谢 BorisCallens,这正是我想要的。 Phil Haack Post 正是我所需要的。谢谢 。我搜索了一段时间,但始终找不到答案。 A+++ 在这里的答案。感谢其他人如何尝试帮助我,非常感谢。谢谢。
    • 很高兴我能提供服务;)
    【解决方案2】:

    不确定您在寻找什么:

    您似乎缺少一个问题表:

    question
    ---------
    question_id
    question_text
    

    然后将其链接到用户

    user_question
    --------------
    user_id
    question_id
    answer
    

    【讨论】:

    • 否,该问题只能与一位用户关联
    【解决方案3】:

    错误在于

    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserQuestion)%>
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(1).UserQuestion)%>
    

    生成 HTML 代码:

    <input id="UserQuestion" name="UserQuestion" type="text" value="UserQuestion" />
    <input id="UserQuestion" name="UserQuestion" type="text" value="UserQuestion" />
    

    如您所见:有相同ids 的输入,这是被禁止的。因此,当您将数据发送回控制器时,仅通过 PostParameters 发送一个值:UserQuestion=、无索引、无前缀、无UserQuestions.UserQuestion[0]
    如需深入了解类似问题,我建议使用Fiddler 并拦截您的表单发送的流量。

    您的代码的第二个问题是您正在使用UpdateModel(User, "UserInfo") 和UserInfo 参数(用户类型)。 (顺便说一句:这段代码是如何编译的?无论如何它不应该)。 调用UpdateModel时最好使用FormCollection作为参数。因为在您的情况下,变量 UserInfo 已经更新。
    至于我,你正在做一些奇怪的事情,试图编辑整个集合。

    【讨论】:

    • 好的 忘记了你绝对正确的UpdateModel。但是现在,我应该在 textboxFor model.UserInfo.UserQuestions.ElementAt(0).UserQuestion)%> 中放入的内容替换为 ....
    【解决方案4】:

    将代码替换为:

    <h2>Question Creation</h2>
    <% for (int i = 0; i < Model.UserInfo.UserQuestions.Count; i++) { %>
      <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions[i].UserQuestion)%>
      <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions[i].UserAnswer)%>
    <% } %>
    

    那么至少你的代码是干的。

    [HttpPost] 公共无效选择问题(用户用户信息) { IUserRepository repUser = new UserRepository(EntityFactory.GetEntity()); 用户 usr = repUser.GetUserByID(Convert.ToInt32(Session["UserID"]));

    UpdateModel(usr);
    EntityFactory.GetEntity().SaveChanges();
    

    }

    取决于是否存在问题,这应该有效。 UserInfo 对象应该包含您的问题,您可以foreach 他们并手动添加它们。

    【讨论】:

    • 我,谢谢你的回复。但这里的主要问题是我不能做 model.UserInfo.UserQuestions[i] .. 这不是一张表……这是一个 UserQuestions 的实体集合。这意味着我将无法直接在表上指定一个索引,如 [i]。我可以指定它的唯一方法是使用 elementAt(i)... 为什么。
    【解决方案5】:

    您似乎在一个不包含任何更改的新实体上运行 SaveChanges,而不是包含由客户端更新的信息的实体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多