【问题标题】:Many to many relation between Identity and custom table. EF7 - Code first身份和自定义表之间的多对多关系。 EF7 - 代码优先
【发布时间】:2016-11-21 03:21:13
【问题描述】:

如何在 Identity 3.0 中的 AspNetRoles 和我的自定义表之间建立多对多关系?我想要简单的 3 表,同时具有 PermissionId 和 RoleId,例如 AspNetUsersRole。我有这样的事情:

public class Permission
{
    public int PermissionId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ApplicationRole> Roles { get; set; }
}

public class ApplicationRole : IdentityRole
{
    public virtual ICollection<Permission> Permissions { get; set; }
}

但是当我想添加迁移时,我得到了错误:

 Unable to determine the relationship represented by navigation property 'ApplicationRole.Permissions' of type 'ICollection<Permission>'. Either manually configure the relationship, or ignore this property from the model. 

【问题讨论】:

标签: asp.net asp.net-mvc entity-framework asp.net-identity


【解决方案1】:

EF Core (EF7) 目前不支持多对多关系没有连接实体。 (Reference)

所以,你应该做的是为连接表创建一个实体类并映射两个单独的一对多关系。喜欢;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<PostTag>()
        .HasKey(t => new { t.PostId, t.TagId });

    modelBuilder.Entity<PostTag>()
        .HasOne(pt => pt.Post)
        .WithMany(p => p.PostTags)
        .HasForeignKey(pt => pt.PostId);

    modelBuilder.Entity<PostTag>()
        .HasOne(pt => pt.Tag)
        .WithMany(t => t.PostTags)
        .HasForeignKey(pt => pt.TagId);
}

public class PostTag
{
        public int PostId { get; set; }
        public Post Post { get; set; }

        public string TagId { get; set; }
        public Tag Tag { get; set; }
}

【讨论】:

    【解决方案2】:

    关于this question answer,这样可以更轻松-

    class Photo
    {
        public int Id { get; set; }
        public ICollection<PersonPhoto> PersonPhotos{ get; set; }
    }
    
    class PersonPhoto
    {
        public int PhotoId { get; set; }
        public Photo Photo { get; set; }
    
        public int PersonId { get; set; }
        public Person Person { get; set; }
    }
    
    class Person
    {
        public int Id { get; set; }
        public ICollection<PersonPhoto> PersonPhotos{ get; set; }
    }
    

    请务必使用复合键配置PersonPhoto

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PersonPhoto>().HasKey(x => new { x.PhotoId, x.PersonId });
    }
    

    要导航,请使用 Select:

    // person.Photos
    var photos = person.PersonPhotos.Select(c => c.Photo);
    

    【讨论】:

      【解决方案3】:

      添加这个命名空间-

      using Microsoft.AspNetCore.Identity;
      
      
      public class Permission
      {
           [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
           public int PermissionId { get; set; }
           public string Name { get; set; }
           public string UserIdFK { get; set; } //Foreign Key of Identity tbl
      
           [ForeignKey("UserIdFK")]
           public IdentityUser UserDetail { get; set; }
      }
      

      就是这样,快乐编码:)

      【讨论】:

        猜你喜欢
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 2017-02-02
        • 2013-05-02
        相关资源
        最近更新 更多