ASP.NET MVC IDENTITY_INSERT 设置为 OFF
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否创建了 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.