如何在两个对象之间建立连接,引用一个对象,并与EF Core中的许多对象建立连接

发布于 2025-02-09 19:43:06 字数 1251 浏览 1 评论 0原文

我有两个类

class Role
{ 
    string Id { get; set; }
    string Name { get; set; }
}

class Permission
{ 
    string Id { get; set; }
    string Name { get; set; }
}

要创建一个表格,以分配多个权限

class RoleAssignedPermissions
{ 
    string Id { get; set; }
    Role Role { get; set; }
    ICollection<Permission> Permissions { get; set; }
}

on ModeLcreating我拥有的角色:

modelBuilder.Entity<Role>(entity =>
            {
                entity.HasKey(x => x.Id);
            });
modelBuilder.Entity<Permission>(entity =>
            {
                entity.HasKey(x => x.Id);
            });
modelBuilder.Entity<RoleAssignedPermissions>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.HasOne(x => x.Role);                    
                entity.HasMany(x => x.Permissions);
            });

在这种情况下是通过添加迁移的三个表,角色表可以使用ID和名称,

但是权限而不是ID和名称,出现新列roleassignedPermissionsId,以及在Table roleassignedpermissions ID和Royid中,

但应在cod> cod> roleassignedpermissiondedpermissions < /code>一列<代码>权限

I have two classes

class Role
{ 
    string Id { get; set; }
    string Name { get; set; }
}

class Permission
{ 
    string Id { get; set; }
    string Name { get; set; }
}

I want to create a table for assign multiple permission to a role

class RoleAssignedPermissions
{ 
    string Id { get; set; }
    Role Role { get; set; }
    ICollection<Permission> Permissions { get; set; }
}

In OnModelCreating I have:

modelBuilder.Entity<Role>(entity =>
            {
                entity.HasKey(x => x.Id);
            });
modelBuilder.Entity<Permission>(entity =>
            {
                entity.HasKey(x => x.Id);
            });
modelBuilder.Entity<RoleAssignedPermissions>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.HasOne(x => x.Role);                    
                entity.HasMany(x => x.Permissions);
            });

In this case is by adding a migration are generated three tables, roles table is ok with id and name

But Permissions instead of id and name, appear new column RoleAssignedPermissionsId, and in table RoleAssignedPermissions id and roleId

But should be in RoleAssignedPermissions a column for collection of Permissions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2025-02-16 19:43:06

这是实体。你可以这样做

class Role
{ 
    string Id { get; set; }
    string Name { get; set; }
    public virtual ICollection<Permission> Permissions {get; set;}
}

class Permission
{ 
    string Id { get; set; }
    string Name { get; set; }
    [ForeignKey("Role")]
    public string RoleId {get; set;}
    public virtual Role Role {get; set;}
}

Here are the entities. You can do like this

class Role
{ 
    string Id { get; set; }
    string Name { get; set; }
    public virtual ICollection<Permission> Permissions {get; set;}
}

class Permission
{ 
    string Id { get; set; }
    string Name { get; set; }
    [ForeignKey("Role")]
    public string RoleId {get; set;}
    public virtual Role Role {get; set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文