locked
.NET Core SqlException: Invalid object name 'IdentityUserClaim'. when logging in RRS feed

  • Question

  • User297458589 posted

    I have 2 applications:

    ManagementStudio and DocumentStudio. ManagementStudio consists of several dlls that DocumentStudio references.

    When I try to login to DocumentStudio, I get this error:

    enter image description here

    I'm not sure whats going on because I have included IdentityUserClaim already in the list of my models:

    [Table("IdentityUserClaim", Schema = "ManagementStudio")]
    public class UserClaims : IdentityUserClaim<string>
    {
        [Key]
        public override int Id { get; set; }
    }

    enter image description here

    public DbSet<ApplicationUsers> ApplicationUsers { get; set; }
    public DbSet<UserRoles> UserRoles { get; set; }
    public DbSet<UserClaims> UserClaims { get; set; }
    public DbSet<IdentityUserClaim<string>> IdentityUserClaim { get; set; }
    public DbSet<IdentityUserRole<string>> IdentityUserRole { get; set; }
    public DbSet<Applications> Applications { get; set; }
    public DbSet<Roles> Roles { get; set; }
    public DbSet<ApiAccess> ApiAccess { get; set; }
    public DbSet<EventLogs> EventLogs { get; set; }
    public DbSet<ActivityLogs> ActivityLogs { get; set; }
    public DbSet<CommunicationLogs> CommunicationLogs { get; set; }
    public DbSet<UploadLogs> UploadLogs { get; set; }
    public DbSet<Repositories> Repositories { get; set; }
    public DbSet<Emails> Emails { get; set; }
    public DbSet<Assets> Assets { get; set; }
    public DbSet<Announcements> Announcements { get; set; }
    public DbSet<AnnouncementAttachments> AnnouncementAttachments { get; set; }

    I noticed that my CustomClaimsHelper is being called so I'm including it as well:

    public class CustomClaimsCookieSignInHelper<TIdentityUser> where TIdentityUser : IdentityUser
        {
    
            private readonly SignInManager<TIdentityUser> _signInManager;
    
            public CustomClaimsCookieSignInHelper(SignInManager<TIdentityUser> signInManager)
            {
                _signInManager = signInManager;
            }
    
            public async Task SignInUserAsync(TIdentityUser user, bool isPersistent, IEnumerable<Claim> customClaims)
            {
                var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
                var identity = claimsPrincipal.Identity as ClaimsIdentity;
                var claims = (from c in claimsPrincipal.Claims select c).ToList();
                var savedClaims = claims;
                if (customClaims != null)
                {
                    identity.AddClaims(customClaims);
                }
                await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme,
                    claimsPrincipal,
                    new AuthenticationProperties { IsPersistent = isPersistent });
            }
        }

    I'm not sure what else I can do to make this work.

    EDIT:

    I have tested manually creating a table named IdentityUserClaim with the same columns as my ManagementStudio.IdentityUserClaim table and it works. That means if I remove the Schema for IdentityUserClaim, the app will work. However, is there a way to force DocumentStudio to look for the proper Schema?

    Wednesday, June 26, 2019 4:47 AM

All replies

  • User-1764593085 posted

    Hi StardocsSVC,

    public DbSet<UserClaims> UserClaims { get; set; }
    public DbSet<IdentityUserClaim<string>> IdentityUserClaim { get; set; }

    Why do you have both? And your Table attribute is applied on the UserClaims model.

    Have you tried to use fluent api directly?

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUserClaim<string>>()
                .ToTable("IdentityUserClaim", schema: "ManagementStudio");
          
        }

    Best Regards,

    Xing

    Wednesday, June 26, 2019 7:07 AM
  • User297458589 posted

    Hi Xing, 

    This is my DbContext in full. If I remove the IdentityUserClaim, the same error appears

    public class ManagementStudioDbContext : DbContext
    {
        public ManagementStudioDbContext(DbContextOptions<ManagementStudioDbContext> options) : base(options){}
        public DbSet<ApplicationUsers> ApplicationUsers { get; set; }
        public DbSet<UserRoles> UserRoles { get; set; }
        public DbSet<UserClaims> UserClaims { get; set; }
        public DbSet<IdentityUserClaim<string>> IdentityUserClaim { get; set; }
        public DbSet<IdentityUserRole<string>> IdentityUserRole { get; set; }
        public DbSet<Applications> Applications { get; set; }
        public DbSet<Roles> Roles { get; set; }
    
        UniqueKeyGenerator uniqueKeyGenerator = new UniqueKeyGenerator();
        
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema(schema: Environment.GetEnvironmentVariable(MSCASGlobals.MS_DatabaseSchema));
    
            Applications application = new Applications
            {
                Id = Guid.NewGuid().ToString(),
                Name = "ManagementStudio",
                Description = "System Management Tool",
                IsActive = true,
                SecretKey = uniqueKeyGenerator.GenerateUniqueKey(),
                CreatedOn = DateTime.Now,
                UpdatedOn = DateTime.Now
            };
    
            Applications application2 = new Applications
            {
                Id = Guid.NewGuid().ToString(),
                Name = "DocumentStudio",
                Description = "Document Management Tool",
                IsActive = true,
                SecretKey = uniqueKeyGenerator.GenerateUniqueKey(),
                CreatedOn = DateTime.Now,
                UpdatedOn = DateTime.Now
            };
    
            ApplicationUsers user = new ApplicationUsers
            {
                Id = Guid.NewGuid().ToString(),
                UserName = "system@managementstudio.com",
                NormalizedUserName = "SYSTEM@MANAGEMENTSTUDIO.COM",
                Email = "system@managementstudio.com",
                NormalizedEmail = "SYSTEM@MANAGEMENTSTUDIO.COM",
                SecurityStamp = Guid.NewGuid().ToString(),
                PhoneNumber = "1234567890",
                FirstName = "System",
                LastName = "Administrator",
                CreatedOn = DateTime.Now,
                UpdatedOn = DateTime.Now
            };
    
            Roles role = new Roles
            {
                Id = Guid.NewGuid().ToString(),
                Name = "SystemAdministrator",
                NormalizedName = "SYSTEMADMINISTRATOR",
                ApplicationId = application.Id
            };
    
            Roles role2 = new Roles
            {
                Id = Guid.NewGuid().ToString(),
                Name = "User",
                NormalizedName = "USER",
                ApplicationId = application.Id
            };
    
            Roles role3 = new Roles
            {
                Id = Guid.NewGuid().ToString(),
                Name = "SystemAdministrator",
                NormalizedName = "SYSTEMADMINISTRATOR",
                ApplicationId = application2.Id
            };
            
            Roles role4 = new Roles
            {
                Id = Guid.NewGuid().ToString(),
                Name = "User",
                NormalizedName = "USER",
                ApplicationId = application2.Id
            };
            
            UserRoles userRole = new UserRoles
            {
                Id = Guid.NewGuid().ToString(),
                RoleId = role.Id,
                UserId = user.Id
            };
    
            UserRoles userRole2 = new UserRoles
            {
                Id = Guid.NewGuid().ToString(),
                RoleId = role2.Id,
                UserId = user.Id
            };
    
            UserRoles userRole3 = new UserRoles
            {
                Id = Guid.NewGuid().ToString(),
                RoleId = role3.Id,
                UserId = user.Id
            };
    
            UserRoles userRole4 = new UserRoles
            {
                Id = Guid.NewGuid().ToString(),
                RoleId = role4.Id,
                UserId = user.Id
            };
    
            //https://www.learnentityframeworkcore.com/migrations/seeding
            PasswordHasher<ApplicationUsers> passwordHasher = new PasswordHasher<ApplicationUsers>();
            user.PasswordHash = passwordHasher.HashPassword(user, "P@ssw0rd");
    
            modelBuilder.Entity<UserClaims>()
            .ToTable("IdentityUserClaim", schema: "ManagementStudio");
            modelBuilder.Entity<UserRoles>()
            .ToTable("IdentityUserRole", schema: "ManagementStudio");
            modelBuilder.Entity<Applications>().HasData(application);
            modelBuilder.Entity<Applications>().HasData(application2);
            modelBuilder.Entity<ApplicationUsers>().HasData(user);
            modelBuilder.Entity<Roles>().HasData(role);
            modelBuilder.Entity<Roles>().HasData(role2);
            modelBuilder.Entity<Roles>().HasData(role3);
            modelBuilder.Entity<Roles>().HasData(role4);
            modelBuilder.Entity<UserRoles>().HasKey(k => new { k.UserId, k.RoleId });
            modelBuilder.Entity<UserRoles>().HasData(userRole);
            modelBuilder.Entity<UserRoles>().HasData(userRole2);
            modelBuilder.Entity<UserRoles>().HasData(userRole3);
            modelBuilder.Entity<UserRoles>().HasData(userRole4);
            modelBuilder.Entity<IdentityUserRole<string>>().HasKey(k => new { k.UserId, k.RoleId });
            base.OnModelCreating(modelBuilder);
        }
    
        
    }

    Wednesday, June 26, 2019 7:26 AM