3

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"/>

            }
Breece
  • 31
  • 1
  • 1
  • 3
  • Very strange indeed. Nothing points that you have an identity column, and still you have that error... Can you show the code to create the obj Customer object? – Renato Afonso Feb 06 '17 at 09:17
  • I added the html code that initiates the Submit action (fill the customer object) – Breece Feb 07 '17 at 08:33
  • The error clearly comes from nested object related to `obj`. This can't be answered without seeing the entire object graph that's added. – Gert Arnold Apr 26 '21 at 19:48

1 Answers1

0

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

https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx

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

  • I do not mention identity colum either in my C# code and my SQL table property. But the error forces me to deal with this problem. – Breece Feb 06 '17 at 02:31
  • @Breece, I edited to post to inform that If you do not have identity column on yout table, set DatabaseGenerated to *None*. Can you provide more details about your table and EF mapping? – Marcos Jonatan Suriani Feb 06 '17 at 02:37
  • Thank you. But the problem persists. The customercode is a string. – Breece Feb 06 '17 at 09:12