我正在将使用成员资格的现有 Web 应用程序移植到使用身份的新应用程序。我已经更新了所有表定义,在VS Community Edition 2015版本14.0.25431.01 Update 3中创建了一个新的MVC 5 Web应用程序,然后从旧项目中引入了代码,以及我在Adam Freeman书中的项目中尝试过的一些代码。
但是用户管理器等似乎想要两个dbo。用户和 dbo。AspNetUsers.
如何初始化身份应用程序DBContext,以便UserManager,SignInManager等可以从数据库中读取?任何帮助都会很棒。
目前,当"用户">表命名为"用户"时,我收到以下错误:
Server Error in '/' Application.
Invalid object name 'dbo.AspNetUsers'.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.AspNetUsers'.
Line 150: User tempUser = await UserManager.FindAsync(model.UserName, model.Password);
当表被命名为"AspNetUsers"时,我得到错误:
Invalid object name 'dbo.Users'.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Users'.
作为参考,标识对象不会从模板定义中更改:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("IdentityEFContext", throwIfV1Schema: false)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
ApplicationUserManager manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
...
}
public class ApplicationUser : User
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
传统的 EF 代码有效,例如
private IdentityEFContext dbContext
{
get
{
return HttpContext.GetOwinContext().Get<IdentityEFContext>();
}
}
List<Activity> Activities = dbContext.Activities.ToList();
List<User> Users = dbContext.Users.ToList();
标识外部的上下文定义首先是标准 EF 代码:
public class IdentityEFContext : IdentityDbContext<User>
{
public IdentityEFContext() : base("IdentityEFContext") { }
//public DbSet<UserLogin> UserLogins { get; set; }
//public DbSet<UserClaim> UserClaims { get; set; }
//public DbSet<UserRole> UserRoles { get; set; }
public DbSet<Activity> Activities { get; set; }
//public DbSet<User> Users { get; set; }
//public DbSet<User> AppUsers { get; set; }
...
}
用户的表名称是使用 Fluent 定义的
public class UserConfig : EntityTypeConfiguration<User>
{
public UserConfig()
{
ToTable("AspNetUsers");
HasKey(u => u.Id);
...
}
}
想知道这是否相关。我尝试了以下内容,应用程序DBContext仅包含角色和用户。这应该提供所有需要的信息,但我想我会报告。
ApplicationDbContext db1 = context.Get<ApplicationDbContext>(); // contains ROLES And USERS
IdentityEFContext db2 = context.Get<IdentityEFContext>(); // contains all tables for all defined dbsets
UserModel
public class User : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> 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;
}
}
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext()
: base("IdentityEFContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>()
.ToTable("User");
}
}
应用程序用户管理器
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
ApplicationUserManager manager = new ApplicationUserManager(new UserStore<User>(context.Get<ApplicationDbContext>()));
...
}