IMG-LOGO

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

andy - 10 Jan, 2014 6048 Views 1 Comment

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

Amit
11 Dec, 2018
Nice Thanks My issue resolve it .
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles