外键组件' RoleId'不是类型' ApplicationUserRole'的声明属性。

时间:2016-06-22 12:28:51

标签: entity-framework-6 asp.net-web-api2

1.这是模型创建功能

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("TEST");

        modelBuilder.Entity<ApplicationUserLogin>().Map(c =>
        {
            c.ToTable("UserLogin");
            c.Properties(p => new
            {
                p.UserId,
                p.LoginProvider,
                p.ProviderKey
            });
        }).HasKey(p => new { p.LoginProvider, p.ProviderKey, p.UserId });

         //Mapping for ApiRole
        modelBuilder.Entity<ApplicationRole>().Map(c =>
        {
            c.ToTable("Role");
            c.Property(p => p.Id).HasColumnName("RoleId");
            c.Properties(p => new
            {
                p.Name
            });
        }).HasKey(p => p.Id);
        modelBuilder.Entity<ApplicationRole>().HasMany(c => c.Users).WithRequired().HasForeignKey(c => c.RoleId);

        modelBuilder.Entity<ApplicationUser>().Map(c =>
        {
            c.ToTable("User");
            c.Property(p => p.Id).HasColumnName("UserId");
            c.Properties(p => new
            {
                p.AccessFailedCount,
                p.Email,
                p.EmailConfirmed,
                p.PasswordHash,
                p.PhoneNumber,
                p.PhoneNumberConfirmed,
                p.TwoFactorEnabled,
                p.SecurityStamp,
                p.LockoutEnabled,
                p.LockoutEndDateUtc,
                p.UserName
            });
        }).HasKey(c => c.Id);
        modelBuilder.Entity<ApplicationUser>().HasMany(c => c.Logins).WithOptional().HasForeignKey(c => c.UserId);
        modelBuilder.Entity<ApplicationUser>().HasMany(c => c.Claims).WithOptional().HasForeignKey(c => c.UserId);
        modelBuilder.Entity<ApplicationUser>().HasMany(c => c.Roles).WithRequired().HasForeignKey(c => c.UserId);

        modelBuilder.Entity<ApplicationUserRole>().Map(c =>
        {
            c.ToTable("UserRole");
            c.Properties(p => new
            {
                p.UserId,
                p.RoleId
            });
        })
        .HasKey(c => new { c.UserId, c.RoleId });


        modelBuilder.Entity<ApplicationUserClaim>().Map(c =>
        {
            c.ToTable("UserClaim");
            c.Property(p => p.Id).HasColumnName("UserClaimId");
            c.Properties(p => new
            {
                p.UserId,
                p.ClaimValue,
                p.ClaimType
            });
        }).HasKey(c => c.Id);
    }

我收到错误: &#34;外键组件&#39; RoleId&#39;不是类型&#39; ApplicationUserRole&#39;的声明属性。验证它是否未从模型中明确排除,并且它是有效的原始属性。&#34;

ApplicationUserRole正在从以下类扩展:

      //   TKey:
public class IdentityUserRole<TKey>
{
    public IdentityUserRole();

    // Summary:
    //     RoleId for the role
    public virtual TKey RoleId { get; set; }
    //
    // Summary:
    //     UserId for the user that is in the role
    public virtual TKey UserId { get; set; }
}

为什么我会收到此错误?

我已经阅读了一些SO线程,该状态,使用Model binder(代码优先)标识或映射扩展属性,并声明要在派生类中覆盖这些属性。即使我这样做了,也会发生同样的错误。

已完成ApplicationUserRole实体映射以解决用户和角色实体之间的多对多关系。

0 个答案:

没有答案