简体   繁体   English

SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'AspNetUsers'中的Identity列插入显式值

[英]SqlException: Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF

I have changed aspnetuser table column datatype string to long using enable-migration of Microsoft.AspNet.Identity. 我已使用Microsoft.AspNet.Identity的enable-migration将aspnetuser表的表列数据类型字符串更改为long。

Table successfully created using following script of migration : 使用以下迁移脚本成功创建了表:

CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.Long(nullable:false,identity:true),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
FullName = c.String(nullable: false, maxLength: 50),
CreatedBy = c.Long(nullable: true),
CreatedDate = c.DateTime(),
ModifiedDate = c.DateTime(nullable: false, defaultValue: null, defaultValueSql: null),
ModifiedBy = c.Long(nullable: true),
IsDeleted = c.Boolean(nullable: false, defaultValue: false, defaultValueSql: "0")
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");

But when i try to create user using following code getting error as "SqlException: Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF". 但是,当我尝试使用以下代码创建用户时,由于“ SqlException:当IDENTITY_INSERT设置为OFF时,无法在表'AspNetUsers'中为身份列插入显式值”。

// first we create Admin rool
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = SuperAdminRole;
roleManager.Create(role);

//Here we create a Admin super user who will maintain the website               

var user = new ApplicationUser();
user.UserName = "Admin";
user.Email = "xyz@gmail.com";
user.FullName = "xyz";
user.IsDeleted = false;
user.ModifiedBy = null;
user.ModifiedDate = null;
user.CreatedBy = string.Empty;
user.CreatedDate = DateTime.Now;

string userPWD = "admin_123";
var chkUser = UserManager.Create(user, userPWD);

If anybody have solution for this please share. 如果有人对此有解决方案,请分享。

Thanks for quick response! 感谢您的快速回复!

I believe that IDENTITY_INSERT is auto increment functionality and it is set to OFF. 我相信IDENTITY_INSERT是自动递增功能,并且设置为OFF。 So, verify the field 'Id' is identity column in the sql database and add the below attribute in the 'Id' column in the code first entity framework 因此,验证字段“ Id”是sql数据库中的标识列,并在代码优先实体框架的“ Id”列中添加以下属性

[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'Ogretmenler' 中为标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Ogretmenler' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表''中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table '' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“任务”中插入标识列的显式值 - SqlException: Cannot insert explicit value for identity column in table 'Tasks' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'Recipe'中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Recipe' when IDENTITY_INSERT is set to OFF ASP.NET MVC 错误 SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“用户”中插入标识列的显式值 - ASP.NET MVC error SqlException: Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF EF Core SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“MenuCategories”中插入标识列的显式值 - EF Core SqlException: Cannot insert explicit value for identity column in table 'MenuCategories' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF ASP.NET Core 2.1 时,无法在表“类别”中插入标识列的显式值 - SqlException: Cannot insert explicit value for identity column in table 'Categories' when IDENTITY_INSERT is set to OFF ASP.NET Core 2.1 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表'Table'中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表'[table]'中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM