Structure of the IDENTITY_INSERT Object

The category table in the database is defined as follows:

CategoryID - PK ,identity specification ON  (only this column has identity)   
Description  
Title  

I want to insert new data:

Category cat = new Category
{
    Description = "xxx",
    Title = "yyy",
};
if (cat.EntityState == EntityState.Detached)
{
   Articlerctx.AddToCategories(cat);
}
return Articlerctx.SaveChanges();

I get an error:

You cannot insert an explicit value for the identity column in the Categories table if IDENTITY_INSERT is set to OFF.

But I do not insert CategoryID! I want it to get auto value!

+3
source share
2 answers

Your script works very well in my case - using EF4.

Check out the following things:

  • Have you changed your database model since you created the Entity model, and if so, have you really updated your entity model?

  • cat.EntityState detached ?? EF4 ObjectContext Added. Cat .

+4

. StoreGeneratedPattern on CategoryID Identity, None. .

+5

Source: https://habr.com/ru/post/1784955/


All Articles