我想控制分配给标识用户的主键或Id列的内容。基本上,我的系统的用户也是另一个系统的用户(要求),我想在我的系统上创建该用户的帐户时使用来自其他系统的用户ID。让它们相同可以简化很多事情,而不是需要同时支持来自其他系统的用户ID和来自我的不同的用户ID。
到目前为止,我已将Id的类型从字符串更改为long,以匹配。但是,当我创建用户时,新用户创建失败,因为Id为null。也就是说,IdentityUser期望Id列是Identity,因此插入时数据库会自动填充它。基本上,它在调用Create(用户,密码)之前忽略了我设置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需要是Identity列。
所以在这一点上,我不知道接下来要尝试什么。这甚至可能吗?
在离开这一段时间之后,我回来做了一些研究并解决了这个问题。基本上,我必须使用None而不是Computed或Identity来装饰DatabaseGeneratedAttribute。
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public override long Id
{
get { return base.Id; }
set { base.Id = value; }
}