简体   繁体   中英

Entity Framework error: Cannot insert explicit value for identity column in table

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF 设计器

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm getting this error on EF.

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

Any ideas?

Here's some code:

GroupMember groupMember = new GroupMember();
            groupMember.GroupId = group.Id;
            groupMember.UserId = (new UserId(group.Owner));
            //groupMember.Id = _groupContext.GroupMembers.Count();
            group.GroupMembers.Add(groupMember);

            _groupContext.SaveChanges();

数据库

EF设计师

I'm using DB first and the table has identity column. I didn't use the db-scaffolding to generate this, I copied it from another entity and by mistake I took this property with.

So

Try to check the DBContext Class. I got this error, and the issue was with this property ".ValueGeneratedNever()" I have just removed it and it works fine,

modelBuilder.Entity<TableName>(entity =>
        {
            entity.Property(e => e.Id)
                //.ValueGeneratedNever()
                .HasColumnName("ID");
        });

Note: a moderator deleted this answer as a duplicate and left my other answer up, on a question with only the sql-server tag (which was the first question I arrived at from google). Since this question has the entity framework tag, posting the answer again here.

Using the wrong property to specify a foreign key causes Entity Framework to demote the primary key to ... something else. Entity Framework will then always attempt to insert an explicit value, which throws a database exception because it can't insert the value it's been told is a primary key and shouldn't be inserted.

Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'
Inner Exception:
SqlException: Cannot insert explicit value for identity column in table 'FOO' when IDENTITY_INSERT is set to OFF.

If you don't want to use EF core's auto-generating primary key values feature, you can turn it off. You can add your data to the primary key It should resolve the error - Set Identity Insert off

[Key]

[DatabaseGenerated(DatabaseGeneratedOption.None)]

public int StudentId { get; set; }

Setting Database Generation option to None helped me. You can find more about it here- https://docs.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations

Add thisline in order to allow the Id column to receive 1,2,3 and 4 values instead of being auto-numbered.

Sql("SET IDENTITY_INSERT MembershipTypes ON")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM