Error while trying to access Claims Identity Core

asp.net-core entity-framework-core

Question

I getting this error while I'm trying to access the claims from Entity Framework Core.

Invalid column name 'UserId1'.

what is strange is that I don't have a UserId1 in the class.

My User Class looks like:

public partial class User : IdentityUser
{
     public string FirstName {get;set;}
     public string LastName {get;set;}

     ... other properties

    public virtual ICollection<UserClaim> UserClaim { get; set; }
}

My User claims class look like:

public partial class UserClaim : Microsoft.AspNetCore.Identity.IdentityUserClaim<string>
{
    public byte[] Version { get; set; }

    public virtual User User { get; set; }
}

I'm actually getting the error on this line:

var userClaims = await _userManager.GetClaimsAsync(user);
1
0
3/25/2020 5:38:46 PM

Accepted Answer

Configure your model like below:

public partial class User : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<UserClaim> UserClaim { get; set; }
}
public partial class UserClaim : Microsoft.AspNetCore.Identity.IdentityUserClaim<string>
{
    public byte[] Version { get; set; }
}

DbContext:

public class ApplicationDbContext : IdentityDbContext<User, IdentityRole, string, UserClaim, IdentityUserRole<string>, IdentityUserLogin<string>,IdentityRoleClaim<string>,IdentityUserToken<string>>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<User>(b =>
        {
            // Each User can have many UserClaims
            b.HasMany(e => e.UserClaim)
                .WithOne()
                .HasForeignKey(uc => uc.UserId)
                .IsRequired();
        });
    }
}
1
3/26/2020 8:05:06 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow