首页 文章

Identity User的数据库定义 - dbo.AspNetUsers和dbo.Users

提问于
浏览
1

我正在移植一个现有的webapp,它使用Membership来创建一个使用Identity的新应用程序 . 我已经更新了所有表定义,在VS Community Edition 2015中创建了一个新的MVC 5 Web应用程序,版本为14.0.25431.01 Update 3,然后从旧项目中引入代码,以及我在Adam Freeman的项目中尝试过的一些代码书 .

But UserManager etc seem to want both dbo.Users and dbo.AspNetUsers.

如何初始化Identity ApplicationDBContext以便UserManager,SignInManager等可以从数据库中读取?任何援助都会很棒 .

目前,当Users表名为“Users”时,我收到以下错误:

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);

当表格按here的名称"AspNetUsers"命名时,我收到错误消息:

Invalid object name 'dbo.Users'.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Users'.

作为参考,Identity对象不会从模板定义中更改:

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();

Identity之外的上下文定义首先是标准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);
        ...
    }
}

不知道这是否相关 . 我尝试了以下操作,applicationDBContext只包含角色和用户 . 这应该提供所有需要的信息,但我想我会报告 .

ApplicationDbContext db1 = context.Get<ApplicationDbContext>(); // contains ROLES And USERS
IdentityEFContext db2 = context.Get<IdentityEFContext>();       // contains all tables for all defined dbsets

1 回答

  • 2

    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");
        }
    }
    

    ApplicationUserManager

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        ApplicationUserManager manager = new ApplicationUserManager(new UserStore<User>(context.Get<ApplicationDbContext>()));
        ...
    }
    

相关问题