ASP.NET MVC IDENTITY_INSERT 设置为 OFF

发布于 2024-10-10 16:31:51 字数 2302 浏览 2 评论 0原文

我的 HomeController 中有以下代码:

public ActionResult Create()
        {
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")]Article articleToCreate)
        {
            if (!ModelState.IsValid)
                return View();

            try
            {
                _db.AddToArticleSet(articleToCreate);
                articleToCreate.posted = DateTime.Now;
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

这是 Create 方法的视图

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <div id="create">

        <h2>Create News Article <span>( <%=Html.ActionLink("Cancel", "Index") %> )</span></h2>

        <% using (Html.BeginForm()) {%>

            <fieldset>
                <legend>Fields</legend>
                <p>
                    <label for="headline">Headline</label>
                    <%= Html.TextBox("headline") %>
                </p>
                <p>
                    <label for="story">Story <span>( HTML Allowed )</span></label>
                    <%= Html.TextArea("story") %>
                </p>
                <p>
                    <label for="image">Image URL</label>
                    <%= Html.TextBox("image") %>
                </p>
                <p>
                    <input type="submit" value="Post" />
                </p>
            </fieldset>

        <% } %>

    </div>
    <!-- END div#create -->

</asp:Content>

当我尝试提交数据时遇到的问题是一个 InnerException,表示 IDENTITY_INSERT 设置为关闭。然而,我的文章表中的ID(称为storyId)设置为自动增量,并且还设置为身份和主键/索引。

我该如何解决这个问题?谢谢。

I have the following code in my HomeController:

public ActionResult Create()
        {
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")]Article articleToCreate)
        {
            if (!ModelState.IsValid)
                return View();

            try
            {
                _db.AddToArticleSet(articleToCreate);
                articleToCreate.posted = DateTime.Now;
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

And this is the view for the Create Method

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <div id="create">

        <h2>Create News Article <span>( <%=Html.ActionLink("Cancel", "Index") %> )</span></h2>

        <% using (Html.BeginForm()) {%>

            <fieldset>
                <legend>Fields</legend>
                <p>
                    <label for="headline">Headline</label>
                    <%= Html.TextBox("headline") %>
                </p>
                <p>
                    <label for="story">Story <span>( HTML Allowed )</span></label>
                    <%= Html.TextArea("story") %>
                </p>
                <p>
                    <label for="image">Image URL</label>
                    <%= Html.TextBox("image") %>
                </p>
                <p>
                    <input type="submit" value="Post" />
                </p>
            </fieldset>

        <% } %>

    </div>
    <!-- END div#create -->

</asp:Content>

The problem I get when I try and submit the data is an InnerException saying that the IDENTITY_INSERT is set to off. However the ID in my article table (which is called storyId) is set to Auto Increment, and is also set as Identity and also as primary key/index.

How do I fix this problem? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-10-17 16:31:51

您是否创建了 Linq To SQL DBML 文件,然后将数据库架构更改为全部以进行自动增量?我认为您可能已更改架构但忘记更新 linq 设计器。

只需删除设计器中的表并重新将其从数据库中拖动即可。

Did you create the Linq To SQL DBML file and then change the Database Schema to all for Auto Increment? I'm thinking that you may have changed the schema but forgot to update the linq designer.

Just delete the table in the designer and re-drag it from the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文