问题描述
我创建了一个自定义字段,并尝试将自定义字段插入aspnetusers表中,但是未插入其他字段数据。下面是我尝试的代码:
var identityUser = new createuser
{
Email = model.Email,UserName = model.Email,TenantId = obj.ToString(),IsAdmin= true
};
var result = await _userManager.CreateAsync(identityUser,model.Password);
var assignRole = await _userManager.AddToRoleAsync(identityUser,"ADMIN");
班级:
public class createuser:IdentityUser
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string OrganizationName { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
public string TenantId { get; set; }
public bool IsAdmin { get; set; }
}
DbContext:
public class ApplicationDBContext:IdentityDbContext<IdentityUser>
{
public ApplicationDBContext(DbContextOptions options) : base(options)
{
}
}
启动
//Configure Entity Frameworkcore with sql Server
services.AddDbContext<ApplicationDBContext>(options =>
{
options.UsesqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
这里缺少任何内容,只有原始字段会更新,而未插入添加的字段值。
解决方法
此处应为CreateUser:
public class ApplicationDBContext : IdentityDbContext<CreateUser>
{
public ApplicationDBContext(DbContextOptions options) : base(options)
{
}
}
Startup.cs:
services.AddIdentity<CreateUser,IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddDefaultUI();
依赖注入:
public class HomeController : Controller
{
private readonly UserManager<CreateUser> _userManager;
public HomeController(UserManager<CreateUser> userManager)
{
_userManager = userManager;
}
//...
}
自定义的详细步骤可以在Customize the model文档中找到。