gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:45 26 4
gpt4 key购买 nike

我正在将一个使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com