Table 'tblCustomer' does not have the identity property. Cannot perform SET operation

Occurs when I try to set the identity_insert to ON in the SQL DATAMANAGEMENT STUDIO.

The worst is I do not know why should I deal with this value, and I am not even sure that the following code hits more than the copy of the database in the local memory.

I have got this message previously, when the debug hits the SaveChanges line : "Cannot insert explicit value for identity column in table 'tblCustomer' when IDENTITY_INSERT is set to OFF"

 ` public ActionResult Submit( Customer obj) //validation runs { if (ModelState.IsValid) { CustomerDal Dal = new CustomerDal(); Dal.Customer.Add(obj); //in memory Dal.SaveChanges(); //physical commit return View("Customer", obj); } else { return View("EnterCustomer", obj); } }` public class Customer { [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.none)] public string CustomerCode { get; set; } [Required] [StringLength(10)] [RegularExpression("^[A-Z]{7,7}$")] public string CustomerName { get; set; } } public class CustomerDal : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Customer>().ToTable("tblCustomer"); } public DbSet<Customer> Customer { get; set; } } 

CREATE TABLE [dbo].[tblCustomer]( [CustomerCode] [varchar](50) NOT NULL, [CustomerName] [varchar](50) NULL, CONSTRAINT [PK_tblCustomer] PRIMARY KEY CLUSTERED ( [CustomerCode] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]

 @using (Html.BeginForm("Submit", "Customer", FormMethod.Post)) { <i>Customer Name : </i> @Html.TextBoxFor(m => m.CustomerName) <br> @Html.ValidationMessageFor(x => x.CustomerName) <br> <i>Customer Code : </i> @Html.TextBoxFor(m => m.CustomerCode) <br> @Html.ValidationMessageFor(x => x.CustomerCode) <br> <input id = "Submit1" type = "submit" value = "submit"/> } 
3

1 Answer

Assuming that your table tblCustomer has an identity column, and assuming that you are configuring mappings using notations, specify the DatabaseGenerated for the identity property:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } 

If you do not have identity column on yout table, set it to None.

[DatabaseGenerated(DatabaseGeneratedOption.None)] public int Id { get; set; } 

(v=vs.113).aspx

Can you provide more details about your table and EF mapping?

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like