简体   繁体   中英

Entity Framework Core incorrectly querying relationships

I have an existing ASP.NET MVC app that uses Identity 2.0. I'm trying to query the user objects using a new Core 2.1 app with Ef Core 2.1.

I'm querying directly rather than using UserManager / RoleManager as the .NET MVC and .NET core apps have different versions and I don't want to get myself into any trouble down the track.

My problem is that I cannot get all the users in a particular role.

I am trying to do itvlike this in my .net core application:

public partial class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    { }

    public virtual DbSet<ApplicationUser> AspNetUsers { get; set; }
    public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
    public virtual DbSet<AspNetUserRole> AspNetUserRoles { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<AspNetUserRole>()
                        .HasKey(pc => new { pc.UserId, pc.RoleId });

    }
}

My model to map the roles:

public class AspNetRole
{
    [Key]
    public Guid Id { get; set; }

    [MaxLength(256)]
    [Required]
    public string Name {get; set;}

    public virtual ICollection<AspNetUserRole> AspNetUserRoles {get; set;}

}

My model to map the users:

 public class ApplicationUser : IdentityUser
{

    public virtual ICollection<AspNetUserRole> AspNetUserRoles {get; set;}

}

and my join table:

public class AspNetUserRole
{

    [MaxLength(256)]
    [Required]
    public Guid UserId { get; set; }

    public ApplicationUser User {get; set;}

    [MaxLength(256)]
    [Required]
    public Guid RoleId { get; set; }

    public AspNetRole Role {get; set;} 

}

The query that I am running in my repository is this:

    var usersInRole = _context.AspNetRoles
        .Where(p => p.Name == "Manager")
        .SelectMany(p => p.AspNetUserRoles)
        .Select(pc => pc.User);

However the query is failing. EF is translating is as follows (I've taken out a bunch of fields from the SELECT statement):

SELECT [p.AspNetUserRoles.User].[Id], [p.AspNetUserRoles.User].[UserName] FROM [AspNetRoles] AS [p] INNER JOIN [AspNetUserRoles] AS [p.AspNetUserRoles] ON [p].[Id] = [p.AspNetUserRoles].[RoleId] LEFT JOIN [AspNetUsers] AS [p.AspNetUserRoles.User] ON [p.AspNetUserRoles].[UserId1] = [p.AspNetUserRoles.User].[Id] WHERE [p].[Name] = @__role_0

As you can see it's incorrectly querying [p.AspNetUserRoles].[UserId1] and therefore gives the following error:

System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'UserId1'.

You need to add following code in addition to your code in OnModelCreating method of ApplicationDbContext class

modelBuilder.Entity<AspNetUserRole>()
    .HasOne(aur => aur.User)
    .WithMany(aur => aur.AspNetUserRoles)
    .HasForeignKey(aur => aur.UserId);

modelBuilder.Entity<AspNetUserRole>()
    .HasOne(aur => aur.Role)
    .WithMany(aur => aur.AspNetUserRoles)
    .HasForeignKey(aur => aur.RoleId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM