Johseffer
Johseffer

Reputation: 53

Entity Framework Identity invalid column name "*_ID" when try to FindByName

I created my application based on the Identity Framework, and create an interface ApplicationUser to set the User_ID type to GUID. All it's ok, but when I try to load the user using the UserManager class, I get an exception that I cannot understand. I cannot understand why this column is added in the command

The exception:

ORA-00904: "Extent1"."ApplicationUser_Id": identificador inválido

The command that causes the exception:

SELECT 
    "Extent1"."ID_USER_CLAIM" AS "ID_USER_CLAIM", 
    "Extent1"."ID_USER" AS "ID_USER", 
    "Extent1"."CLAIM_TYPE" AS "CLAIM_TYPE", 
    "Extent1"."CLAIM_VALUE" AS "CLAIM_VALUE", 
    "Extent1"."ApplicationUser_Id" AS "ApplicationUser_Id"
FROM 
    "BEECORE"."ADM_USER_CLAIM" "Extent1"
WHERE 
    ("Extent1"."ID_USER" = :p__linq__0)

The code:

var user = userService.UserManager.FindByName(model.UserName);

ApplicationUser:

using Microsoft.AspNet.Identity.EntityFramework;
using System;

namespace Bee.Core.Domain.Identity
{
    public class ApplicationUser : IdentityUser<Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
    {
        public ApplicationUser()
        {
            Id = Guid.NewGuid();
        }
        public ApplicationUser(string name) : this() { UserName = name; }
    }
}

Configuration:

using Bee.Core.Domain.Identity;
using System.Data.Entity.ModelConfiguration;

namespace Bee.Core.Administration.Data.EntityConfig.Identity
{
    public class IdentityUserConfiguration : EntityTypeConfiguration<ApplicationUser>
    {
        public IdentityUserConfiguration()
        {
            ToTable("ADM_USER");

            HasKey(c => new
            {
                c.Id
            });

            Property(c => c.Id).HasColumnName("ID_USER");
            Property(c => c.Email).HasColumnName("EMAIL");
            Property(c => c.EmailConfirmed).HasColumnName("EMAIL_CONFIRMED");
            Property(c => c.PasswordHash).HasColumnName("PASSWORD_HASH");
            Property(c => c.SecurityStamp).HasColumnName("SECURITY_STAMP");
            Property(c => c.PhoneNumber).HasColumnName("PHONE_NUMBER");
            Property(c => c.PhoneNumberConfirmed).HasColumnName("PHONE_NUMBER_CONFIRMED");
            Property(c => c.TwoFactorEnabled).HasColumnName("TWO_FACTOR_ENABLED");
            Property(c => c.LockoutEndDateUtc).HasColumnName("LOCKOUT_ENDDATEUTC");
            Property(c => c.LockoutEnabled).HasColumnName("LOCKOUT_ENABLED");
            Property(c => c.AccessFailedCount).HasColumnName("ACCESS_FAILED_COUNT");
            Property(c => c.UserName).HasColumnName("USER_NAME");
        }
    }
}

Upvotes: 1

Views: 1465

Answers (1)

julianorinaldi
julianorinaldi

Reputation: 146

I had the same problem when I have implemented custom Microsoft Identity. In that case, I needed to configure all foreign keys explicitly in EntityTypeConfiguration for each Identity class/table.

For example, your code need into class User (inherit - IdentityUser):

        HasMany(x => x.Claims)
                .WithRequired(y => y.ApplicationUser);
        HasMany(x => x.Roles)
                .WithRequired(y => y.ApplicationUser);
        HasMany(x => x.Logins)
                .WithRequired(y => y.ApplicationUser);

You need configure all the class Microsoft Identity with Foreign Key:

Class UserClaim (inherit - IdentityUserClaim):

        HasRequired(x => x.ApplicationUser)
                .WithMany(y => y.Claims)
                    .HasForeignKey(f => f.UserId);

Class Role (inherit - IdentityRole):

        HasMany(c => c.Users).WithRequired().HasForeignKey(c => c.RoleId);

Class UserLogin (inherit - IdentityUserLogin):

        HasRequired(x => x.ApplicationUser)
                .WithMany(y => y.Logins)
                    .HasForeignKey(f => f.UserId);

Class UserRole (inherit - IdentityUserRole):

        HasRequired(x => x.ApplicationUser)
                .WithMany(y => y.Roles)
                    .HasForeignKey(f => f.UserId);

I hope have help you.

Upvotes: 2

Related Questions