如何读取 dbo.AspNetUsers table?
How do I read from the dbo.AspNetUsers table?
我想从 Identity-table dbo.AspNetUsers
中检索数据,但我还不知道如何查询它。
这是我的数据库上下文:
public class ProjectsDbContext : IdentityDbContext<IdentityUser>
{
public ProjectsDbContext(DbContextOptions<ProjectsDbContext> options) : base(options) { }
public DbSet<Project> Projects { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new NullReferenceException();
}
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Project>()
.HasMany(c => c.ChildProjects)
.WithOne(p => p.ParentProject)
.HasForeignKey(p => p.ParentProjectId);
}
}
这是我的 User
-class:
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
这是我的查询,它不起作用:
List<User> projectOwners = await db.Users.ToListAsync();
我收到的错误信息是:
Cannot implicitly convert type 'System.Collections.Generic.List<Microsoft.AspNetCore.Identity.IdentityUser>' to 'System.Collections.Generic.List<Projects.Models.User>'
如果我在查询中用 var
替换 List<User>
,错误就会消失,但我得到的集合不包含我自己 User
-[ 的任何属性=37=].
如何访问 AspNetUsers
,包括我自己在 User
中定义的额外属性?
假设您正在或多或少地根据标准样板设置您的项目,我相信您可以像这样强制转换身份用户的覆盖类型:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// pretty standard initialisation so far
services.AddDbContext<ProjectsDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = false) // this is the key - put your custom User Identity here
.AddEntityFrameworkStores<ProjectsDbContext>();
}
ProjectsDbContext.cs
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ProjectsDbContext: IdentityDbContext<User> // this is your custom User type
{
public ProjectsDbContext(DbContextOptions<ProjectsDbContext> options)
: base(options)
{
}
}
那么,由于您要更改数据模型,因此需要迁移架构:
PM> Add-Migration change_IdentityUser
PM> Update-Database
您会注意到迁移将您的自定义字段添加到 table:
public partial class change_IdentityUser : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "FirstName",
table: "AspNetUsers",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "LastName",
table: "AspNetUsers",
nullable: true);
}
.......
最后,所有对 SignInManager
/UserManager
和其他助手的引用都需要使用新的泛型类型参数进行更新:
_LoginPartial.cshtml
@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager
使用 EF Core 时,table AspNetUsers 被映射到 Users DbSet,因此您可以通过
轻松引用它
db.Users
如果您自定义 AspNetTable,添加您自己的字段,例如 FirstName、LastName,那么您需要根据 @timur 的回答从 IdentityUser
派生,但您仍然应该能够引用 table 仅 db.Users
我想从 Identity-table dbo.AspNetUsers
中检索数据,但我还不知道如何查询它。
这是我的数据库上下文:
public class ProjectsDbContext : IdentityDbContext<IdentityUser>
{
public ProjectsDbContext(DbContextOptions<ProjectsDbContext> options) : base(options) { }
public DbSet<Project> Projects { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new NullReferenceException();
}
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Project>()
.HasMany(c => c.ChildProjects)
.WithOne(p => p.ParentProject)
.HasForeignKey(p => p.ParentProjectId);
}
}
这是我的 User
-class:
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
这是我的查询,它不起作用:
List<User> projectOwners = await db.Users.ToListAsync();
我收到的错误信息是:
Cannot implicitly convert type 'System.Collections.Generic.List<Microsoft.AspNetCore.Identity.IdentityUser>' to 'System.Collections.Generic.List<Projects.Models.User>'
如果我在查询中用 var
替换 List<User>
,错误就会消失,但我得到的集合不包含我自己 User
-[ 的任何属性=37=].
如何访问 AspNetUsers
,包括我自己在 User
中定义的额外属性?
假设您正在或多或少地根据标准样板设置您的项目,我相信您可以像这样强制转换身份用户的覆盖类型:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// pretty standard initialisation so far
services.AddDbContext<ProjectsDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = false) // this is the key - put your custom User Identity here
.AddEntityFrameworkStores<ProjectsDbContext>();
}
ProjectsDbContext.cs
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ProjectsDbContext: IdentityDbContext<User> // this is your custom User type
{
public ProjectsDbContext(DbContextOptions<ProjectsDbContext> options)
: base(options)
{
}
}
那么,由于您要更改数据模型,因此需要迁移架构:
PM> Add-Migration change_IdentityUser
PM> Update-Database
您会注意到迁移将您的自定义字段添加到 table:
public partial class change_IdentityUser : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "FirstName",
table: "AspNetUsers",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "LastName",
table: "AspNetUsers",
nullable: true);
}
.......
最后,所有对 SignInManager
/UserManager
和其他助手的引用都需要使用新的泛型类型参数进行更新:
_LoginPartial.cshtml
@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager
使用 EF Core 时,table AspNetUsers 被映射到 Users DbSet,因此您可以通过
轻松引用它db.Users
如果您自定义 AspNetTable,添加您自己的字段,例如 FirstName、LastName,那么您需要根据 @timur 的回答从 IdentityUser
派生,但您仍然应该能够引用 table 仅 db.Users