Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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
Asp.net 如何防止控制器向数据库发送标识列_Asp.net_Sql Server_Entity Framework_Razor_Asp.net Mvc 5 - Fatal编程技术网

Asp.net 如何防止控制器向数据库发送标识列

Asp.net 如何防止控制器向数据库发送标识列,asp.net,sql-server,entity-framework,razor,asp.net-mvc-5,Asp.net,Sql Server,Entity Framework,Razor,Asp.net Mvc 5,控制器操作: [HttpPost] [ValidateAntiForgeryToken] public ActionResult New_Shelf(Shelf shelf) { shelf.User_Id = int.Parse(Request.Cookies["UserId"].Value); db_shelf.Shelves.Add(shelf); db_shelf.SaveChanges();

控制器操作:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult New_Shelf(Shelf shelf)
    {
        shelf.User_Id = int.Parse(Request.Cookies["UserId"].Value);
        db_shelf.Shelves.Add(shelf);

            db_shelf.SaveChanges();

        return RedirectToAction("Index", "Home");
    }
型号:

namespace Book_shelf.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;


public partial class Shelf
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public int Size { get; set; }
        [ForeignKey("Id")]
        public int User_Id { get; set; }
    }
}
视图:

@model Book\u shelf.Models.shelf
@使用(Html.BeginForm(“New_Shelf”,“Home”,FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
名称

@Html.TextBoxFor(a=>a.Name)

大小
@Html.TextBoxFor(a=>a.Size)

添加 }
错误:

当>identity\u insert设置为OFF时,无法在表“Shelf”中为identity列插入显式值

我不熟悉ASP.NETMVC和实体框架。
我正在尝试为我的表“Shelf”插入新行。但在将数据发布到操作时,视图似乎也在发送Id。我尝试在操作中记录Shelf.Id的值,该值为“0”。如何防止视图在Id属性中发送任何数据?

问题出在Shelf.edmx文件中,我在表设计器中检查了
Id
列的属性,并将
storedGeneratedPattern
值设置为
identity
,该值以前设置为
none

检查:如果它是
identity
列,数据库将为其生成一个值,您不需要插入它。如果您需要将数据显式插入标识列,则需要在插入后使用诸如SET identity\u insert dbo.TableName ON的命令SET identity\u insert dbo.TableName OFFNo@Srini131我不想将数据显式插入标识列。插入时表中的数据避免标识列名,以便数据库本身生成标识列值,因为它是一个autonumberOh该死!我很抱歉:D@Rigerta
 @model Book_shelf.Models.Shelf

 @using (Html.BeginForm("New_Shelf", "Home", FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)

                <label style="font-family:sans-serif">Name</label>
                <br />
                @Html.TextBoxFor(a => a.Name)
                <br />
                <br />
                <label style="font-family:sans-serif">Size</label>
                <br />
                @Html.TextBoxFor(a => a.Size)
                <br />
                <br />

                <button class="btn btn-sm"  type="submit"> Add</button>

            }