我创建了一个自定义字段,并试图在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;
}
//...
}
自定义的详细步骤可以在自定义模型文档中找到。