Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我在尝试向数据库添加新注释时出错_C#_Asp.net Mvc - Fatal编程技术网

C# 我在尝试向数据库添加新注释时出错

C# 我在尝试向数据库添加新注释时出错,c#,asp.net-mvc,C#,Asp.net Mvc,我遇到一个错误,上面写着: INSERT语句与外键约束“FK_dbo.Comments_dbo.Posts_PostID”冲突。冲突发生在数据库“MVCProjectApp.Models.DBPostsContext”、表“dbo.Posts”、列“PostID”中。 声明已终止 我迷路了,不知道该怎么办 下面是创建注释 @model MVCProjectApp.Models.Comment @{ ViewBag.Title = "Create Comment"; } <h2&

我遇到一个错误,上面写着:

INSERT语句与外键约束“FK_dbo.Comments_dbo.Posts_PostID”冲突。冲突发生在数据库“MVCProjectApp.Models.DBPostsContext”、表“dbo.Posts”、列“PostID”中。
声明已终止

我迷路了,不知道该怎么办

下面是创建注释

@model MVCProjectApp.Models.Comment

@{
    ViewBag.Title = "Create Comment";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Comments</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Message)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Message)
            @Html.ValidationMessageFor(model => model.Message)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Timestamp)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Timestamp)
            @Html.ValidationMessageFor(model => model.Timestamp)
        </div>


        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "~/FullPost/Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
下面是注释模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCProjectApp.Models
{
    public class Post
    {
        public int PostID { get; set; }
        public string Title { get; set; }
        public string Message { get; set; }
        public DateTime Timestamp { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }
    public class Comment
    {
        public int CommentID { get; set; }
        public int PostID { get; set; }
        public string Username { get; set; }
        public string Message { get; set; }
        public DateTime Timestamp { get; set; }
        public virtual Post Post { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
名称空间MVCProjectApp.Models
{
公营职位
{
公共int PostID{get;set;}
公共字符串标题{get;set;}
公共字符串消息{get;set;}
公共日期时间时间戳{get;set;}
公共虚拟ICollection注释{get;set;}
}
公开课评论
{
public int CommentID{get;set;}
公共int PostID{get;set;}
公共字符串用户名{get;set;}
公共字符串消息{get;set;}
公共日期时间时间戳{get;set;}
公共虚拟Post{get;set;}
}
}
我迷路了,不知道该怎么办

你应该看看你得到的错误。它说你正试图插入一条评论。DBMS在插入之前运行的外键约束检查尝试确保注释具有它知道的PostID。您没有设置它,因此它将默认为0,这在Posts表中不存在,因此会出现错误


您应该在调用
SaveChanges()
之前设置注释的PostID,这可以通过在视图中添加
@Html.HiddenFor(m=>m.PostID)
字段来完成。然后在控制器中设置模型的PostID。

您需要将注释与post关联。最简单的解决方案是在视图中添加post id的隐藏字段。

如何传递post id变量,当我转到注释时,它在url中page@Scott这取决于你的网址。例如,您可以使用
Request.QueryString[“ProjectID”]
。如何在控制器中设置postid我尝试了@html.hiddenfor(model=>model.postid),但是数据库没有创建任何内容隐藏字段在表单中,对吗<应在注释模型中设置代码>PostID。如果这不起作用,您可以从数据库加载
Post
对象,并将
注释添加到
Post.Comments
集合中。它表示您作为外键提供的postid在其主表中不存在。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCProjectApp.Models
{
    public class Post
    {
        public int PostID { get; set; }
        public string Title { get; set; }
        public string Message { get; set; }
        public DateTime Timestamp { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }
    public class Comment
    {
        public int CommentID { get; set; }
        public int PostID { get; set; }
        public string Username { get; set; }
        public string Message { get; set; }
        public DateTime Timestamp { get; set; }
        public virtual Post Post { get; set; }
    }
}