首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'AspNetUsers‘中的标识列插入显式值

问SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'AspNetUsers‘中的标识列插入显式值
EN

Stack Overflow用户
提问于 2018-05-15 13:57:25
回答 1查看 1.5K关注 0票数 0

我已经使用Microsoft.AspNet.Identity的启用迁移将aspnetuser表列数据类型字符串更改为long。

使用以下迁移脚本成功创建表:

代码语言:javascript
复制
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");

但是当我尝试使用以下代码创建用户时,得到的错误是"SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'AspNetUsers‘中的标识列插入显式值“。

代码语言:javascript
复制
// 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);

如果有人对此有解决方案,请分享。

感谢您的快速响应!

EN

回答 1

Stack Overflow用户

发布于 2018-05-15 14:25:00

我相信IDENTITY_INSERT是自动递增功能,它被设置为OFF。因此,验证字段'Id‘是sql数据库中的identity列,并在代码优先实体框架的'Id’列中添加以下属性

代码语言:javascript
复制
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50343185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档