如何控制IdentityUser.Id列的值?
How to control IdentityUser.Id column values?
我想控制分配给身份用户的主键或 Id 列的内容。基本上,我系统的用户也是另一个系统的用户(要求),我想在我的系统上创建该用户的帐户时使用其他系统的用户 ID。让它们相同会简化很多事情,而不是需要同时支持来自其他系统的用户 ID 和来自我的不同的用户 ID。
到目前为止,我已将 Id 的类型从字符串更改为长整数,以进行匹配。但是,当我创建用户时,新用户创建失败,因为 Id 为空。也就是说,IdentityUser 期望 Id 列是一个身份,因此它会在插入时由数据库自动填充。基本上,它忽略了我在调用 Create(user, password) 之前设置的 Id 值。我试过将 Id 属性 覆盖为
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Computed)]
public override long Id
{
get { return base.Id; }
set { base.Id = value; }
}
但出现以下异常:
[NotSupportedException: Modifications to tables where a primary key column has property 'StoreGeneratedPattern' set to 'Computed' are not supported. Use 'Identity' pattern instead. Key column: 'Id'. Table: 'CodeFirstDatabaseSchema.ApplicationUser'.]
System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildSetClauses(DbExpressionBinding target, PropagatorResult row, PropagatorResult originalRow, TableChangeProcessor processor, Boolean insertMode, Dictionary`2& outputIdentifiers, DbExpression& returning, Boolean& rowMustBeTouched) +2292
System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildInsertCommand(PropagatorResult newRow, TableChangeProcessor processor) +204
System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler) +412
这基本上就是说 Id 需要是一个标识列。
所以此时我不知道接下来要尝试什么。这甚至可能吗?
暂时摆脱这个问题后,我回来做了更多的研究并解决了这个问题。基本上,我必须用 None 而不是 Computed 或 Identity.
来装饰 DatabaseGeneratedAttribute
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public override long Id
{
get { return base.Id; }
set { base.Id = value; }
}
我想控制分配给身份用户的主键或 Id 列的内容。基本上,我系统的用户也是另一个系统的用户(要求),我想在我的系统上创建该用户的帐户时使用其他系统的用户 ID。让它们相同会简化很多事情,而不是需要同时支持来自其他系统的用户 ID 和来自我的不同的用户 ID。
到目前为止,我已将 Id 的类型从字符串更改为长整数,以进行匹配。但是,当我创建用户时,新用户创建失败,因为 Id 为空。也就是说,IdentityUser 期望 Id 列是一个身份,因此它会在插入时由数据库自动填充。基本上,它忽略了我在调用 Create(user, password) 之前设置的 Id 值。我试过将 Id 属性 覆盖为
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Computed)]
public override long Id
{
get { return base.Id; }
set { base.Id = value; }
}
但出现以下异常:
[NotSupportedException: Modifications to tables where a primary key column has property 'StoreGeneratedPattern' set to 'Computed' are not supported. Use 'Identity' pattern instead. Key column: 'Id'. Table: 'CodeFirstDatabaseSchema.ApplicationUser'.]
System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildSetClauses(DbExpressionBinding target, PropagatorResult row, PropagatorResult originalRow, TableChangeProcessor processor, Boolean insertMode, Dictionary`2& outputIdentifiers, DbExpression& returning, Boolean& rowMustBeTouched) +2292
System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildInsertCommand(PropagatorResult newRow, TableChangeProcessor processor) +204
System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler) +412
这基本上就是说 Id 需要是一个标识列。
所以此时我不知道接下来要尝试什么。这甚至可能吗?
暂时摆脱这个问题后,我回来做了更多的研究并解决了这个问题。基本上,我必须用 None 而不是 Computed 或 Identity.
来装饰 DatabaseGeneratedAttribute[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public override long Id
{
get { return base.Id; }
set { base.Id = value; }
}