2017-03-26 19 views
0

が、私はこのエラーを取得しています使用して、このエラーを取得するは、Entity Frameworkのナビゲーションプロパティ

「役割」は派生しない参照ナビゲーションプロパティを にエンティティタイプ 「Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole」逆のナビゲーション プロパティ 'User'が宣言されているタイプ 'SafeWare.Models.ApplicationUserRole'から。

見に

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

助けてください!

答えて

1

Inverse Properties属性すなわち[InverseProperty("User")][InverseProperty("Role")]ICollection<ApplicationUserRole>のプロパティを宣言するが、それらはApplicationUserRoleクラスにタイプApplicationUserApplicationRoleの特性を指します。

は例えば、タイプApplicationUserなくApplicationUserRole

である

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

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

のポイントをInverseProperty属性を削除しようとしたことがありますか?


あなたがベースRolesメソッドをオーバーライドするために探している場合、それはpublic override ICollection<ApplicationUserRole> Roles { get; }すべきではありません。

は、個人的に私はIdentityUser方法

例えば内Rolesメソッドをオーバーライドするのではなく、基本クラスIdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>に型を渡すことになりますApplicationUser : IdentityUser<TKey, TLogin, ApplicationUserRole, TClaim>

他の同一性モデルにも同じ原則を適用できます。 ApplicationRole

+0

私が間違っていることは分かりません。 IdentityUserRole宣言では、ApplicationRoleとApplicationUserの型が使用されます。さて、これは削除されたIdentityの古いバージョンです。 – chrisdyck

関連する問題