1

一些背景

我的 Asp.Net MVC 4 数据库第一个应用程序、提交和评论中有 2 个表。(还有很多,但它们与这个问题无关)。任何给定的提交都可以有任意数量的评论(一对多)与之关联。

我有一个提交详细信息页面,用户可以在该页面上打开一个带有部分视图的 jQuery 对话框,以向提交添加评论。此部分视图绑定到为添加注释而创建的强类型视图模型。

表格/模型

提交表

这是提交表的类模型。(为简单起见,我省略了一些属性)。

Partial Public Class Submission
    Public Property Submission_ID As Integer
    Public Property Submission_Hash As String
    Public Property Created As Nullable(Of Date)
    Public Property Modified As Date

    Public Overridable Property Comments As ICollection(Of Comment) = New HashSet(Of Comment)
End Class

评论表

这是 Comment 表的类模型。(再次,为简单起见,我省略了一些属性)。

Partial Public Class Comment
    Public Property Comment_ID As Integer
    Public Property User_ID As Nullable(Of Integer)
    Public Property Comment_Type As Nullable(Of Integer)
    Public Property Comment1 As String
    Public Property Created As Nullable(Of Date)
    Public Property Modified As Date

    Public Overridable Property Comment_Type1 As Comment_Type
    Public Overridable Property User As User
    Public Overridable Property Submissions As ICollection(Of Submission) = New HashSet(Of Submission)
End Class

要求

我正在尝试做的是向给定提交的已存在的评论集合中添加新评论。

至今...

到目前为止,我有一个添加注释对话框的视图模型,它在我的控制器中初始化,然后将填充的视图模型返回给控制器的 post 方法。这是代码

视图模型

Public Class SubmissionAddCommentViewModel

    <Display(Name:="Submission")> _
    Public Property Submission_ID As Integer

    <Display(Name:="User")> _
    Public Property User_ID As Integer

    <Display(Name:="Comment type")> _
    Public Property Comment_Type As Integer

    <Required(ErrorMessage:="Please enter a comment.")> _
    <Display(Name:="Comment")> _
    Public Property Comment As String

End Class

视图模型生成器

Public Class SubmissionAddCommentViewModel_Builder

    Implements IModelBuilder(Of SubmissionAddCommentViewModel, Comment)
    ReadOnly db As GeosightEntities
    ReadOnly submission As Submission

    Public Sub New(db As GeosightEntities, submission As Submission)
        Me.db = db
        Me.submission = submission
    End Sub

    Public Function CreateFrom(entity As Comment) As SubmissionAddCommentViewModel Implements IModelBuilder(Of SubmissionAddCommentViewModel, Comment).CreateFrom
        Dim model = New SubmissionAddCommentViewModel()

        model.Submission_ID = submission.Submission_ID
        model.User_ID = GetLoggedIn_ID()

        Return model
    End Function

    Public Function Rebuild(model As SubmissionAddCommentViewModel) As SubmissionAddCommentViewModel Implements IModelBuilder(Of SubmissionAddCommentViewModel, Comment).Rebuild
        Return model
    End Function

    Public Sub Add(model As SubmissionAddCommentViewModel)
        Dim comment As New Comment

        ' Map the ViewModel and Model fields
        comment.Comment_Type1 = If(IsNothing(model.Comment_Type), Nothing, db.Comment_Type.Find(model.Comment_Type))
        comment.User = db.Users.Find(model.User_ID)
        comment.Comment1 = model.Comment
        comment.Modified = Now

        ' Add the comment
        db.Submissions.Find(model.Submission_ID).Comments.Add(comment)
        db.SaveChanges()
    End Sub

End Class

控制器获取方法

Function AddComment(Optional ByVal id As Integer = Nothing) As ActionResult

    Dim commentAddView As New SubmissionAddCommentViewModel
    Dim submission As Submission = db.Submissions.Find(id)
    Dim comment As New Comment

    If IsNothing(submission) Then
        Return HttpNotFound()
    End If

    Me.builder_AddComment = New SubmissionAddCommentViewModel_Builder(db, submission)

    ' Create the instance of the submission add comment view model
    commentAddView = builder_AddComment.CreateFrom(comment)

    If Request.IsAjaxRequest Then
        Return PartialView("AddCommentPartial", commentAddView)
    End If

    Return View(commentAddView)
End Function

控制器 Post 方法

<HttpPost()> _
Function AddComment(ByVal commentAddView As SubmissionAddCommentViewModel) As ActionResult

    Dim comment As New Comment
    builder_AddComment = New SubmissionAddCommentViewModel_Builder(db, db.Submissions.Find(commentAddView.Submission_ID))

    ' Handle invalid comment models
    If Not ModelState.IsValid Then

        If Request.IsAjaxRequest Then
            Return PartialView("AddCommentPartial", commentAddView)
        End If

        Return View(commentAddView)
    End If

    ' Add the comment
    Me.builder_AddComment.Add(commentAddView)

    ' Display the successful message
    If Request.IsAjaxRequest Then
        Return PartialView("AddCommentSuccessPartial", commentAddView)
    End If

    Return RedirectToAction("Details", New With {.id = commentAddView.Submission_ID})

End Function

问题

在测试上述功能时,db.SaveChanges()在 ViewModel 构建器中一直执行成功,但是一旦运行此行,执行就会停止。我不知道发生了什么,因此完全卡住了。

有谁知道A)我是否要以正确的方式添加评论?B)为什么这条线在没有任何错误的情况下停止执行?


更新

感谢 Peter Smith 建议在代码周围放置一个 Try Catch,我不知道为什么我一开始没有想到或这样做。

我现在收到以下错误:

{MySql.Data.MySqlClient.MySqlException (0x80004005): 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 '(SELECT" & vbLf & " Submission_Comment. Submission_ID, " & vbLf & " 附近使用的正确语法Submission_Comment。在 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) 的第 1 行" & vbCrLf & " 在 MySql.Data.MySqlClient.MySqlStream.ReadPacket()" & vbCrLf & " & vbCrLf & " 在 MySql。 Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)" & vbCrLf & " 在 MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)" & vbCrLf & " 在 MySql.Data.MySqlClient .MySqlDataReader.NextResult()" & vbCrLf & " 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior 行为)" & vbCrLf & " 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()" & vbCrLf & " 在 MySql。数据.MySqlClient.MySqlCommand。2 identifierValues, List1 generateValues)" & vbCrLf & " 在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter 适配器)}

4

1 回答 1

1

我设法找到了问题并实施了解决方案。为了帮助可能遇到与我相同问题的其他人,这是我的解决方案。

在我的 MySQL 模式中,Submission 和 Comment 表之间的中间表 (Submission_Comment) 有 2 列...Submission_ID 和 Comment_ID。

当我设置表时,我将这些列设为外键,但忘记将它们设为复合主键。将 2 列设为复合主键解决了我的问题。

于 2012-12-21T14:16:23.973 回答