Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

EntityFramework unable to insert data into SQLServer table

$
0
0

Hi, As above,

error Msg :

System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Customers' when IDENTITY_INSERT is set to OFF.


in SQL Server table, I have setUp:

CustId
Is Identity yes, Identity Increment 1  Identity Seed 1


in the EDMX : I made following changes.

using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    public partial class Customer
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CustId { get; set; }
        public string CustomerName { get; set; }
        public string CustomerPassword { get; set; }
        public string CustomerEmailAddr { get; set; }
        public string CustomerMobileNo { get; set; }
        public Nullable<System.DateTime> CreatedOn { get; set; }
    }
}

----Controller

 [HttpPost]
  public void PostContact([FromBody] Customer contact)
 {
    repository.Add(contact);
  }

public void Add(Customer contact)
{
   try
   {            

      var addedContact = ctx.Customers.Add(contact);
      ctx.SaveChanges();
    }
   catch (Exception ex)
    {
      string err = ex.ToString();
    }         

 }

After making change on Class Customer in EDMX, still cannot insert.

Thanks


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>