[英]Extend IdentityUser, foreign key costraint conflict
I want to use ASP.Net-Identity for user management. 我想使用ASP.Net-Identity进行用户管理。 For this, I want to extend the IdentityUser
class with a few attributes. 为此,我想用一些属性扩展IdentityUser
类。
public class AppUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public int Settings_ID { get; set; }
public string Position { get; set; }
public string CompanyName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
[...]
Now, in my data context
, I want to create just one table for this user model: 现在,在我的data context
,我只想为该用户模型创建一个表:
public class AntContext : IdentityDbContext<AppUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<AntContext>(null);
modelBuilder.Entity<AppUser>().ToTable("AppUsers");
base.OnModelCreating(modelBuilder);
}
public override IDbSet<AppUser> Users { get; set; }
[...]
However, when trying to update-database
, I receive the error: 但是,当尝试update-database
,出现错误:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.AppUsers_dbo.AspNetUsers_Id". The conflict occurred in database "C:\\USERS\\NEUMA\\SOURCE\\REPOS\\MYANTON\\MYANTON\\APP_DATA\\ASPNETDB.MDF", table "dbo.AspNetUsers", column 'Id'.
I understand that AspNetUsers
is a table from IdentityUser
, but how do I create only one table so that there are no conflicts? 我知道AspNetUsers
是IdentityUser
一个表,但是如何仅创建一个表,以免发生冲突?
How do I properly extend the IdentityUser
class and use it in my data context? 如何正确扩展IdentityUser
类并在数据上下文中使用它?
The solution is to not mix contexts. 解决方案是不混合上下文。 Maintain a seperation of concerns. 保持关注点分离。 Use the migration script for Identity and create a new script for the business context (your data context). 将迁移脚本用于Identity并为业务上下文(您的数据上下文)创建一个新脚本。
Add a Users table to the business context that references the name or sub from the IdentityContext. 将用户表添加到引用IdentityContext中名称或子名称的业务上下文中。 Please read my answer here for a quite similar question. 请在这里阅读我的答案,以解决类似的问题。
Also, there is no need to extend the ApplicationUser table. 同样,也不需要扩展ApplicationUser表。 You can use AspNetUserClaims to add this kind of information. 您可以使用AspNetUserClaims添加此类信息。 With a custom type: 使用自定义类型:
new Claim("http://www.myapp.com/Firstname", "Firstname");
Or an existing WIF ClaimType : 或现有的WIF ClaimType :
new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", "Lastname");
As for CompanyName, this may actually be part of the business context. 至于CompanyName,这实际上可能是业务环境的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.