我正在使用.Net Identity进行用户管理。 用户的注册包括保存其他详细信息,例如地址。 地址保存在地址表中,我有一个名为地址簿的关联表,其中包含用户和地址的映射以及其他一些详细信息。 由于地址簿表包含其他列,因此我不能使用多对多关系,而是需要与UserAdddress表建立两个1-多关系。
public class AspNetUser
{
public string Id { get; set; }
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string PhoneNumber { get; set; }
public string UserName { get; set; }
public virtual ICollection<AddressBook> AddressBooks { get; set; }
}
public partial class Address
{
public long AddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public virtual ICollection<AddressBook> AddressBooks { get; set; }
}
public partial class AddressBook
{
[Key]
[Column(Order = 0)]
public long AddressId { get; set; }
[Key]
[Column(Order = 1)]
public string UserId { get; set; }
public int? AddressTypeId { get; set; }
public bool? IsDefault { get; set; }
public string Firstname { get; set; }
public string LastName { get; set; }
public virtual Address Address { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
}
我想在帐户控制器中使用注册方法,该方法是.Net Identity的一部分。 我的应用程序用户和应用程序 DbContext 如下所示(注意:我有 2 个上下文):
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
AddressBooks = new HashSet<AddressBook>();
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
return userIdentity;
}
public virtual ICollection<AddressBook> AddressBooks { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnectionString")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>()
.ToTable("AspNetUsers").HasKey(k => k.Id);
modelBuilder.Entity<Address>()
.HasKey(k => k.AddressId);
modelBuilder.Entity<AddressBook>().HasKey(k =>
new
{
k.UserId,
k.AddressId,
}
);
modelBuilder.Entity<AddressBook>()
.HasRequired(t => t.Address)
.WithMany(t => t.AddressBooks)
.HasForeignKey(t => t.AddressId);
modelBuilder.Entity<AddressBook>()
.HasRequired(t => t.AspNetUser)
.WithMany(t => t.AddressBooks)
.HasForeignKey(t => t.UserId);
}
}
在帐户控制器中,我想在注册时通过调用 await UserManager.CreateAsync(user,password) 传递地址信息和地址簿信息;
我尝试了以下方法,但没有用:
var user = new ApplicationUser()
{
UserName = "NewUser5" ,
Email = "a9@snapon.com"
};
var addr = new Address { Address1 = "5505" , City = "City A" , Zip = "91334" };
var addressBook = new AddressBook() {Address = addr, UserId =
user.Id,Firstname = "ContactFName" , LastName = "ConatactLName" ,
AddressTypeId = 1, IsDefault = true };
user.AddressBooks.Add(addressBook);
IdentityResult result = await UserManager.CreateAsync(user, password );
它给了我错误无效的列名ApplicationUser_Id。 我不确定我做错了什么。
是否可以使用身份保存在两个 1-M 关系中?
这是一个远景,但请尝试调用以下内容。
UserManager<AspNetUser>.CreateAsync(user, password);
它在 .net 核心中对我有用。