我像这样在存储库中添加声明,并且工作正常:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...
modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
modelBuilder.Configurations.Add(new ExperienceMap());
}
但是当我将应用程序用户添加到代码中时,它会抛出下一个代码块下方显示的错误:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...
//Error occurs when I add this
modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers").HasKey<string>(l => l.Id);
modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
modelBuilder.Configurations.Add(new ExperienceMap());
}
截屏:
在第一次 AddOrUpdate 时,我的 UserClaims 存储库中引发了该错误:
public void InsertOrUpdate(IEnumerable<IdentityUserClaim> claims, Expression<Func<IdentityUserClaim, Object>> identifierExpression = null)
{
foreach (var claim in claims)
{
if (identifierExpression != null)
DataAccess.UserClaims.AddOrUpdate(identifierExpression, claim); //where the error occurs
else
DataAccess.UserClaims.AddOrUpdate(claim);
}
DataAccess.SaveChanges();
}
感谢您的帮助!
编辑:默认实体根本没有列。
namespace Microsoft.AspNet.Identity.EntityFramework
{
public class IdentityUserClaim<TKey>
{
public IdentityUserClaim();
public virtual string ClaimType { get; set; }
public virtual string ClaimValue { get; set; }
public virtual int Id { get; set; }
public virtual TKey UserId { get; set; }
}
}
这是 ApplicationUser 实体
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public bool FirstTimeLogin { get; set; }
public bool HasConsent { get; set; }
public DateTime ConsentDate { get; set; }
}
public class ApplicationIdentityContext : IdentityDbContext<ApplicationUser>
{
public ApplicationIdentityContext()
: base("ApplicationDbContext", throwIfV1Schema: false)
{
}
public static ApplicationIdentityContext Create()
{
return new ApplicationIdentityContext();
}
}