使用包含自动增量ID的linq查询将记录插入表中

时间:2015-08-25 04:51:24

标签: c# sql-server asp.net-mvc linq

我在向包含自动增量ID的表中插入记录时遇到问题,

我将使用实体框架向表插入值。

这是我的表创建查询

USE [DB_Name]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Table_Name](
    [Discussion_ID] [int] IDENTITY(1,1) NOT NULL,
    [Discussion_Name] [nvarchar](50) NULL,
    [Discription] [nvarchar](255) NULL,

 CONSTRAINT [PK_Table_Name] PRIMARY KEY CLUSTERED 
(
    [Discussion_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

这是我的查看页面代码

@model albaraka.Models.Table_Name

@{
    ViewBag.Title = "Discussion_Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Discussion_Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>AB_Discussion</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.Discussion_ID)


        <div class="form-group">
            @Html.LabelFor(model => model.Discussion_Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Discussion_Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Discussion_Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Discription, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.Discription, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Discription, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这是模型类

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

    public partial class Table_Name
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]  
        public int Discussion_ID { get; set; }
        public string Discussion_Name { get; set; }
        public string Discription { get; set; }

    }
}

这是控制器类

[HttpGet]
public ActionResult Discussion_Create()
{         
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Discussion_Create(Table_Name discussion)
{

    if (ModelState.IsValid)
    {
        db.Table_Name.Add(discussion);
        db.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(discussion);
}

我想在没有用户输入讨论ID的情况下插入记录,这就是为什么我把它放在隐藏字段中,我该怎么做?

1 个答案:

答案 0 :(得分:0)

您无法为Identity列指定值。它没有意义。您需要做的是使用linq To Entity

插入记录
 var yourTable= new TablName{ Discussion_Name = "Discussion1",Discription ="Some Description" };

 var db = new YourEFContext();

 db.Table_Name.Add(yourTable);

 db.SaveChanges();

框架会将值初始化为0