3

我的 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();
            }
        }

这是创建方法的视图

<%@ 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)设置为 Auto Increment,也设置为 Identity 和主键/索引。

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

4

1 回答 1

3

您是否创建了 Linq To SQL DBML 文件,然后将 Database Schema 更改为 all 以实现 Auto Increment?我在想您可能已经更改了架构但忘记更新 linq 设计器。

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

于 2011-01-05T19:38:16.280 回答