实体框架和角色 [英] Entity Framework and roles

查看:62
本文介绍了实体框架和角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题。我有一个用户,其中可以有很多 Roles ,但是 Roles 是全局的,因此我建立了一个如下表结构:

I have an problem. I have a User which can have many Roles, but the Roles are global, so I have set up a table structure as follows:

CREATE TABLE [dbo].[Users](
    [Id] [nvarchar](128) NOT NULL,
    [UserName] [nvarchar](max) NULL,
    [Email] [nvarchar](max) NULL,
    [DateCreated] [datetime] NOT NULL,
    [DateModified] [datetime] NULL,
    [LastLoginDate] [datetime] NOT NULL,
    [PasswordHash] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.Users] PRIMARY KEY CLUSTERED 

CREATE TABLE [dbo].[UserRoles](
    [UserId] [nvarchar](128) NOT NULL,
    [RoleId] [nvarchar](128) NOT NULL,
 CONSTRAINT [PK_dbo.UserRoles] PRIMARY KEY CLUSTERED 
(
    [UserId] ASC,
    [RoleId] ASC
)

CREATE TABLE [dbo].[Roles](
    [Id] [nvarchar](128) NOT NULL,
    [Name] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.Roles] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)

UserRoles 表格在相应的 Id 列上分别为角色用户表格设置了前键。

The UserRoles table has foriegn keys set up for both the Roles and Users table on the respective Id columns.

在我的代码中,我有这个:

In my code, I have this:

public class User : IUser
{
    public string Id { get; set; }

    // Stripped for brevity

    public IList<UserRole> Roles { get; set; }

    public User()
    {
        this.Id = Guid.NewGuid().ToString();
    }
}

public class UserRole
{
    public string RoleId { get; set; }
    public string UserId { get; set; }
}

public class Role : IRole
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IList<UserRole> Users { get; set; }

    public Role()
    {
        this.Id = Guid.NewGuid().ToString();
    }
}

EntityFramework 可以此类,并且我的映射如下:

EntityFramework is fine with this class and with my mapping as follows:

modelBuilder.Entity<UserRole>().HasKey(m => new { m.UserId, m.RoleId });

所有用于映射的工作。
现在我真正想要的是 User 类具有 Roles 而不是 UserRoles 的列表。

Everything for the mapping works. Now what I would really like, is for the User class to have a list of Roles and not UserRoles.

因此,我希望它看起来像这样:

So, I would like it to look like this:

public class User : IUser
{
    public string Id { get; set; }

    // Stripped for brevity

    public IList<Role> Roles { get; set; }

    public User()
    {
        this.Id = Guid.NewGuid().ToString();
    }
}

我确定您可以在这里看到问题。
有人可以想到一种解决方案,其中我的表结构保持不变,但是我可以访问 Roles 而不是 UserRoles 吗?

I am sure you can see the problem here. Can someone think of a solution where my table structure stays the same, but I can access the Roles instead of the UserRoles?

对不起,如果我的解释不好。

Sorry if my explanation is not good.

推荐答案

我认为您是在追求这样的东西...

I think you're after something like this...

在DbContext中:

In DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasMany(x => x.roles)
                .WithMany(x => x.users)
                .Map(x =>
                {
                    x.MapLeftKey("userId");
                    x.MapRightKey("roleId");
                    x.ToTable("UserRole");
                });
}

现在有了。

class User
{
   ICollection<Role> roles {get; set;}
}

class Role
{
    ICollection<User> users {get; set;}
}

这篇关于实体框架和角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆