尝试FindByName时,实体框架标识无效列名“* _ID”

时间:2017-11-23 17:28:53

标签: entity-framework asp.net-web-api identity

我基于Identity Framework创建了我的应用程序,并创建了一个接口ApplicationUser来将User_ID类型设置为GUID。一切都没问题,但是当我尝试使用UserManager类加载用户时,我得到了一个我无法理解的异常。我无法理解为什么在命令

中添加此列

例外:

  

ORA-00904:“Extent1”。“ApplicationUser_Id”:identificadorinválido

导致异常的命令:

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)

代码:

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; }
    }
}

配置:

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");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

当我实现自定义Microsoft身份时,我遇到了同样的问题。在这种情况下,我需要在每个Identity类/表的EntityTypeConfiguration中显式配置所有外键。

例如,您的代码需要进入类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);

您需要使用外键配置所有类Microsoft Identity:

Class UserClaim(inherit - IdentityUserClaim):

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

类角色(inherit - IdentityRole):

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

类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);

我希望能帮到你。