c# - 身份用户的数据库定义 - dbo.AspNetUsers 和 dbo.Users

标签 c# asp.net-mvc entity-framework asp.net-identity

我正在将一个使用 Membership 的现有网络应用程序移植到一个使用 Identity 的新应用程序。我已经更新了所有表定义,在 VS Community Edition 2015 版本 14.0.25431.01 Update 3 中创建了一个新的 MVC 5 Web 应用程序,然后引入了旧项目中的代码,以及我在 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);

当表按照建议命名为“AspNetUsers”时 here ,我得到错误:

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

最佳答案

用户模型

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

关于c# - 身份用户的数据库定义 - dbo.AspNetUsers 和 dbo.Users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43172193/

相关文章:

c# - 单击有/无回发的 MVC 调用方法

c# - 使用 C# 组合框放置图像和字符串

javascript - 编辑行时如何为 jqgrid 中的字段提供不同的下拉选项

C# POCO T4 模板,生成接口(interface)?

c# - 具有默认 0(零)值的属性索引器

c# - System.Printing 未找到(C#)?

c# - 身份服务器单点注销,也从服务器注销

c# - 在自定义 Html Helper 中获取 Action 的 MvcHtmlString

entity-framework - 在 Objectify 中,如何在不知道父键的情况下通过 ID 加载实体?

c# - LINQ to Entities 不支持 LINQ 表达式节点类型 'Invoke'