简体   繁体   English

无法在web-api 2中创建新用户

[英]cannot create new user in web-api 2

I'm trying to create a new user when receiving a POST request to api/accounts/ in my asp.net web-api 2 project. 我正在尝试在我的asp.net web-api 2项目中收到api/accounts/POST请求时创建一个新用户。

Attempt 1: Create Identity User 尝试1:创建Identity User

I've tried exactly as it was shown in the project template: 我完全按照项目模板中的显示进行了尝试:

// Inside Post() method
IdentityUser newUser = new IdentityUser()
{
    UserName = "username",
};
IdentityResult result = await this.UserManager.CreateAsync(newUser, "password");

System.Data.Entity.Core.UpdateException: Cannot insert the value NULL into column 'Discriminator', table 'MyDb.dbo.AspNetUsers'; System.Data.Entity.Core.UpdateException:无法将值NULL插入“Discriminator”列,表'MyDb.dbo.AspNetUsers'; column does not allow nulls. 列不允许空值。 INSERT fails.\\r\\nThe statement has been terminated. INSERT失败。\\ r \\ n语句已终止。

Attempt 2: Create a User 尝试2:创建User

I've also tried adding my subclassed version of IdentityUser : 我也尝试添加我的IdentityUser子类版本:

// Definition
public class User : IdentityUser
{
    public User() { }
}

// Inside Post() method
User newUser = new User()
{
    UserName = userDto.Email,
};
IdentityResult result = await this.UserManager.CreateAsync(newUser, "password");

System.InvalidOperationException: Mapping and metadata information could not be found for EntityType 'MyProject.Data.User'. System.InvalidOperationException:无法找到EntityType“MyProject.Data.User”的映射和元数​​据信息。

It was working originally and I can't figure out why it isn't anymore. 它最初工作,我无法弄清楚为什么它不再存在。 Originally I added two fields onto my User object (FirstName, Lastname) but I still have the problem after removing them. 最初我在我的User对象(FirstName,Lastname)上添加了两个字段,但删除后仍然存在问题。

对于asp身份2.2,Discriminator列已删除表单数据库,您可以迁移数据库或只是删除列

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM