Fik*_*lin 5 asp.net asp.net-mvc entity-framework asp.net-identity
如何在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; }
}
Run Code Online (Sandbox Code Playgroud)
但是当我想添加迁移时,我收到了错误:
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.
Run Code Online (Sandbox Code Playgroud)
Emr*_*lat 10
EF核心(EF7)目前不支持多对多的关系,而不联接实体.(参考)
因此,您应该做的是为连接表创建实体类并映射两个单独的一对多关系.喜欢;
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; }
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2833 次 |
最近记录: |