"N + 1选择问题"通常被称为对象关系映射(ORM)讨论中的一个问题,我理解它必须为对象中看起来很简单的事情做出大量的数据库查询.世界.
有没有人对这个问题有更详细的解释?
我正在尝试为用户管理管理页面提取所有Identity用户及其相关角色.我认为这相当容易,但显然不是.我尝试过以下解决方案:https://stackoverflow.com/a/43562544/5392786但到目前为止还没有解决.
这是我到目前为止:
ApplicationUser:
public class ApplicationUser : IdentityUser
{
public List<IdentityUserRole<string>> Roles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
的DbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Run Code Online (Sandbox Code Playgroud)
启动标识代码
services.AddIdentity<ApplicationUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
我想要显示列表的Razor Page:
public class IndexModel : PageModel
{
private readonly UserManager<ApplicationUser> userManager;
public IndexModel(UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
public IEnumerable<ApplicationUser> Users { get; set; }
public void OnGetAsync()
{
this.Users = userManager.Users.Include(u => …Run Code Online (Sandbox Code Playgroud) c# mysql entity-framework-core asp.net-core asp.net-core-identity
我在linux上使用dotnet core 1.1,当我想从我的常规dbContext中分离identityContext时,我遇到问题,每当我在startup.cs中运行以下行时 - > configure:
//... some other services
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
//running other database.migrations here + seeding data. But it is the line above that causes problems
Run Code Online (Sandbox Code Playgroud)
因此,该行抛出异常:实体类型'IdentityUserLogin'需要定义主键
我根本不明白这一点,为什么我的工作是给IdentityUserLogin一个主键?它是一个第三方类,我甚至没有触及它.我有以下简单的设置:
namespace EsportshubApi.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public ApplicationDbContext()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和applicationUser:
namespace EsportshubApi.Models.Entities
{
public class ApplicationUser : IdentityUser
{
public ApplicationUser() { }
public …Run Code Online (Sandbox Code Playgroud) 在 EF Core 2.0 中,默认情况下不包含身份导航属性,因此在升级后,我添加了它们。因此,对于 User 和 Role 之间的多对多关系,以及 Role 和 RoleClaim 之间的一对多关系,我添加了以下导航属性:
public class User : IdentityUser
{
[Required]
public string Name { get; set; }
public virtual ICollection<IdentityUserRole<string>> Roles { get; set; }
}
public class Role : IdentityRole
{
[Required]
public string Name { get; set; }
public virtual ICollection<IdentityRoleClaim<string>> Claims { get; set;}
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是增加了一个额外RoleId1关键AspNetRoleClaims表和UserId1对AspNetUserRoles表和所有的GET查询实际使用新的密钥,而不是RoleId和UserId它也存在。