Published on: 10 January, 2014

Entity Framework - Issue cannot insert explicit value for identity column.

Tumblr Digg Google Plus

You may get this error message when you try to insert a value that is actually no need to be inserted as the column field has been set as identity column where the value will be automatically added as incremented value.

Sometimes this problem will occur if you have already modified the database table structure. One thing you can do is to refresh the EDMX file or models on your project file. If you are using code first, you can include the following notation on your object class. See below for example. Note the annotation [Key] represents as Primary Key, while the [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] represent that this field will be an automatic identity generated field.

//Remember to import the following namespace
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public class User {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public decimal UserID { get; set; }
    public string DisplayName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.