ask*_*ker 3 c# entity-framework
数据库中的"类别"表定义如下:
CategoryID - PK ,identity specification ON (only this column has identity)
Description
Title
Run Code Online (Sandbox Code Playgroud)
我想插入新数据:
Category cat = new Category
{
Description = "xxx",
Title = "yyy",
};
if (cat.EntityState == EntityState.Detached)
{
Articlerctx.AddToCategories(cat);
}
return Articlerctx.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
我收到错误:
当IDENTITY_INSERT设置为OFF时,无法在表'Categories'中为identity列插入显式值.
但我没有插入CategoryID!我希望它能获得自动价值!
你的映射是错误的.在StoreGeneratedPattern上CategoryID 应被设置为Identity,但你必须把它设置为None.修复它.