1

Controller Action:

    [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");
    }

Model:

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; }
    }
}

View:

 @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>

            }

Error:

Cannot insert explicit value for identity column in table 'Shelf' when >IDENTITY_INSERT is set to OFF.

I am new to ASP.net MVC and Entity Framework. I am trying to Insert a new row for my table 'Shelf'.But it looks like that the view is sending Id as well when posting data to Action.I Tried to Log the value of shelf.Id in the action and it was '0'.How do i prevent the view to send any data in for Id property?

Bilal Alam
  • 874
  • 10
  • 26
  • 1
    Check here: http://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl The idea is that if it is an `IDENTITY` column, the database will generate a value for it and you don't need to insert it. – Rigerta May 04 '17 at 09:56
  • If you need to insert data explicitly in to the identity column you need use command like SET IDENTITY_INSERT dbo.TableName ON After insertion SET IDENTITY_INSERT dbo.TableName OFF –  May 04 '17 at 10:05
  • No @Srini131 I dont want to insert data explicitly in to the identity column. – Bilal Alam May 04 '17 at 10:09
  • 1
    when inserting data in to table avoid the identity column Name so that database itself generates identity column value bacuase it is a autonumber –  May 04 '17 at 10:12
  • @Rigerta Thank you man you pointed out the exact problem and it solved it.You should have told me that as an answer so i could mark that as the right answer – Bilal Alam May 04 '17 at 10:22
  • I am a woman! :D it's fine as long as it solved the problem! :) – Rigerta May 04 '17 at 10:28
  • 1
    Oh damn! I am so sorry :D @Rigerta – Bilal Alam May 04 '17 at 10:37

1 Answers1

1

The problem was in shelf.edmx file,I checked the properties of Id column in the table designer and set the storedGeneratedPattern value to identity which was previously set to none.

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
Bilal Alam
  • 874
  • 10
  • 26