C# 是否可以将Asp.net标识与自动增量密钥字段一起使用?

C# 是否可以将Asp.net标识与自动增量密钥字段一起使用?,c#,asp.net,entity-framework,asp.net-identity,C#,Asp.net,Entity Framework,Asp.net Identity,我使用自己的EF数据库,使用long作为Id字段。我已经创建了所有实体和相关的自定义存储 但是,在注册新用户时,它试图在调用CreateAsync之前调用SetPasswordHashAsync,这意味着它试图写入一个还不存在的数据库记录,当然它会抛出一个错误 看起来系统希望在写入数据库之前知道Id字段,但在此之前无法知道Id,因为它被设置为自动增量字段 我错过了什么?这可能吗 以下是AccountController的代码(取自MVC项目模板) [HttpPost] [异名] [Validat

我使用自己的EF数据库,使用
long
作为
Id
字段。我已经创建了所有实体和相关的自定义存储

但是,在注册新用户时,它试图在调用
CreateAsync
之前调用
SetPasswordHashAsync
,这意味着它试图写入一个还不存在的数据库记录,当然它会抛出一个错误

看起来系统希望在写入数据库之前知道
Id
字段,但在此之前无法知道Id,因为它被设置为自动增量字段

我错过了什么?这可能吗

以下是AccountController的代码(取自MVC项目模板)

[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务);
返回视图(“显示电子邮件”);
}
加法器(结果);
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}
下面是来自UserManager(来自Identity.core)的代码

    public virtual async Task<IdentityResult> CreateAsync(TUser user, string password)
    {
        ThrowIfDisposed();
        var passwordStore = GetPasswordStore();
        if (user == null)
        {
            throw new ArgumentNullException("user");
        }
        if (password == null)
        {
            throw new ArgumentNullException("password");
        }
        var result = await UpdatePasswordInternal(passwordStore, user, password).WithCurrentCulture();
        if (!result.Succeeded)
        {
            return result;
        }
        return await CreateAsync(user).WithCurrentCulture();
    }
公共虚拟异步任务CreateAsync(用户,字符串密码)
{
ThrowIfDisposed();
var passwordStore=GetPasswordStore();
if(user==null)
{
抛出新的ArgumentNullException(“用户”);
}
如果(密码==null)
{
抛出新的ArgumentNullException(“密码”);
}
var result=wait UpdatePasswordInternal(密码存储、用户、密码)。WithCurrentCulture();
如果(!result.successed)
{
返回结果;
}
返回wait wait CreateAsync(用户).WithCurrentCulture();
}

因此,您可以看到,在调用createUser之前更新密码是预期的过程,但这意味着如何工作?我觉得这似乎是错误的。

您需要显示创建用户的代码。默认情况下,
Id
字段是自动生成的,因此我不确定为什么在``CreateAsync`@trailmax之前有
SetPasswordHashAsync
您希望看到什么代码?90%的代码来自Identity.core程序集,因此我没有编写。我现在已经包含了UserManager中CreateUser的代码。这是一个好问题。实际上我以前从未密切关注过这一点。尽管我的许多项目使用
CreateAncy(用户,密码)
方法没有问题。如果您按照
CreateAsync
的代码并查看
UpdatePasswordInternal
方法,它实际上不会写入DB anywhere,它所做的只是在类对象上设置密码哈希和安全戳guid-此时所有内容都在内存中。并且任何ese operations.UpdatePasswordInternal调用SetPasswordHashAsync,我假设它是一个将PasshHash写入DB的方法。不是这样吗?SetPasswordHashAsync只是为了更新传入的用户吗?所有名为Setxxx的存储方法都是这样吗?如果是的话,哪些存储方法实际上是为了与e DB?
    public virtual async Task<IdentityResult> CreateAsync(TUser user, string password)
    {
        ThrowIfDisposed();
        var passwordStore = GetPasswordStore();
        if (user == null)
        {
            throw new ArgumentNullException("user");
        }
        if (password == null)
        {
            throw new ArgumentNullException("password");
        }
        var result = await UpdatePasswordInternal(passwordStore, user, password).WithCurrentCulture();
        if (!result.Succeeded)
        {
            return result;
        }
        return await CreateAsync(user).WithCurrentCulture();
    }