Entity framework 使用实体框架导航属性获取此错误

Entity framework 使用实体框架导航属性获取此错误,entity-framework,asp.net-identity,Entity Framework,Asp.net Identity,我得到了这个错误 实体类型 “Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole”要将 导航属性“角色”引用不是从类型派生的 “SafeWare.Models.ApplicationUserRole”上的反向导航 已声明属性“User” 打电话的时候 bool ret = _userManager.Users.SingleOrDefault(x => x.UserName == userName) != null;

我得到了这个错误

实体类型 “Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole”要将 导航属性“角色”引用不是从类型派生的 “SafeWare.Models.ApplicationUserRole”上的反向导航 已声明属性“User”

打电话的时候

    bool ret = _userManager.Users.SingleOrDefault(x => x.UserName == userName) != null;
此函数测试用户是否存在

以下是我的物品:

public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base()
    {
        Users = new HashSet<ApplicationUserRole>();
    }
    public ApplicationRole(string name, string description) : base(name)
    {
        this.Description = description;
    }
    public virtual string Description { get; set; }

    [InverseProperty("Role")]
    [DisplayName("Users")]
    public new virtual ICollection<ApplicationUserRole> Users { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public ApplicationUser()
    {
        FileUploads = new HashSet<FileUpload>();
        Roles = new HashSet<ApplicationUserRole>();
    }


    [InverseProperty("User")]
    [DisplayName("Roles")]
    public new virtual ICollection<ApplicationUserRole> Roles { get; set; }
}

public class ApplicationUserRole : IdentityUserRole
{
    public ApplicationUserRole()
    {
    }

    [ForeignKey("Id")]
    public virtual ApplicationUser User { get; set; }
    [ForeignKey("Id")]
    public virtual ApplicationRole Role { get; set; }

}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");
        modelBuilder.Entity<ApplicationRole>().HasKey<string>(r => r.Id).ToTable("AspNetRoles");
        modelBuilder.Entity<ApplicationUserRole>().HasKey(r => new { r.UserId, r.RoleId }).ToTable("AspNetUserRoles");

        modelBuilder.Entity<ApplicationUser>().HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.User).WillCascadeOnDelete(value: false);
        modelBuilder.Entity<ApplicationRole>().HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.Role).WillCascadeOnDelete(value: false);

        //modelBuilder.Entity<ApplicationRole>().HasMany<ApplicationUserRole>((ApplicationRole u) => u.Users);
        //modelBuilder.Entity<ApplicationUser>().HasMany<ApplicationUserRole>((ApplicationUser u) => u.Roles);
    }
}
公共类应用程序角色:IdentityRole
{
公共应用程序角色():base()
{
Users=newhashset();
}
公共应用程序角色(字符串名称、字符串描述):基本(名称)
{
这个。描述=描述;
}
公共虚拟字符串描述{get;set;}
[反向属性(“角色”)]
[显示名称(“用户”)]
公共新虚拟ICollection用户{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
公共应用程序用户()
{
FileUploads=newhashset();
Roles=newhashset();
}
[反向属性(“用户”)]
[显示名称(“角色”)]
公共新虚拟ICollection角色{get;set;}
}
公共类ApplicationUserRole:IdentityUserRole
{
公共应用程序Serrole()
{
}
[外国钥匙(“Id”)]
公共虚拟应用程序用户{get;set;}
[外国钥匙(“Id”)]
公共虚拟应用程序角色{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().ToTable(“AspNetUsers”);
modelBuilder.Entity().HasKey(r=>r.Id).ToTable(“AspNetRoles”);
modelBuilder.Entity().HasKey(r=>new{r.UserId,r.RoleId}).ToTable(“AspNetUserRoles”);
modelBuilder.Entity().HasMany(u=>u.Roles)。WithRequired().HasForeignKey(ur=>ur.User)。WillCascadeOnDelete(值:false);
modelBuilder.Entity().HasMany(r=>r.Users).WithRequired().HasForeignKey(ur=>ur.Role).WillCascadeOnDelete(值:false);
//modelBuilder.Entity()有许多((应用程序角色u)=>u.Users);
//modelBuilder.Entity()有许多((ApplicationUser u)=>u.Roles);
}
}
请帮忙

属性,即
[InverseProperty(“用户”)]
[InverseProperty(“角色”)]
声明类型
ICollection
的属性,但它们指向
ApplicationUserRole
类上类型
ApplicationUser
ApplicationRole
的属性

比如说,

[InverseProperty("User")]
[DisplayName("Roles")]
public new virtual ICollection<ApplicationUserRole> Roles { get; set; }
类型为
ApplicationUser
而不是
ApplicationUserRole

您是否尝试删除
InverseProperty
属性


如果您希望重写基本
角色
方法,那么它不应该是
public override ICollection Roles{get;}

就我个人而言,我会将类型传递给基类
IdentityUser:IUser
,而不是重写
IdentityUser
方法中的
Roles
方法

e、 g.
ApplicationUser:IdentityUser


同样的原则也适用于其他身份模型,例如,
ApplicationRole

我看不出我做错了什么。IdentityUserRole声明使用ApplicationRole和ApplicationUser类型。现在,这是已删除的旧版本的Identity。
[ForeignKey("Id")]
public virtual ApplicationUser User { get; set; }