EF 4.1 Code First多个多对多关系

时间:2012-02-06 16:26:40

标签: asp.net-mvc-3 entity-framework-4.1 ef-code-first

我无法绕过某个代码优先关系。我有三个实体:Group,User,GroupPermission。 GroupPermission实体保存有关与组相关的权限的信息。有三种权限:领导者,编辑者,用户。 GroupPermission表应包含主键ID和权限名称。然后我想要一个看起来像这样的关系表:Id - Group_Id - User_Username - GroupPermission_Id。可以有多个组,多个用户,多个权限。我有很多例子可以帮助我创建一个单独的关系表,但我找不到包含多个关系的任何内容。

以下是我的实体......

用户:

public class User
{
    [Key, StringLength(EntityLength.UsernameLength)]
    public string Username { get; set; }
    [Required, StringLength(EntityLength.NameLength)]
    public string FirstName { get; set; }
    [Required, StringLength(EntityLength.NameLength)]
    public string LastName { get; set; }
    [Required, StringLength(EntityLength.Email)]
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime DateCreated { get; set; }

    public virtual UserPermission UserPermission { get; set; }
    public virtual ICollection<Group> Groups { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
    public virtual ICollection<Issue> Issues { get; set; }
    public virtual ICollection<GroupPermission> GroupPermissions { get; set; } 

    public string FullName
    {
        get { return FirstName + ' ' + LastName; }
    }
}

组:

public class Group
{
    [Key]
    public int Id { get; set; }
    [Required, StringLength(EntityLength.GenericLength)]
    public string Name { get; set; }
    [Required, StringLength(EntityLength.DescriptionLength)]
    public string Description { get; set; }

    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
    public virtual ICollection<GroupPermission> GroupPermissions { get; set; }
}

GroupPermission:

public class GroupPermission
{
    [Key]
    public int Id { get; set; }
    [StringLength(EntityLength.GenericLength)]
    public string Name { get; set; }

    public int GroupId { get; set; }
    public virtual ICollection<Group> Groups { get; set; }
    public int UserId { get; set; }
    public virtual ICollection<User> Users { get; set; }

    public enum Permission
    {
        Leader = 1,
        Editor = 2,
        User = 3
    }
}

使用此结构创建表时,我得到一个具有Id,Name,GroupId和UserId的GroupPermissions表。此表只需要Id和Name。然后,它创建一个GroupPermissionUsers表,其中包含GroupPermissions_Id和User_Username。这是应该是Id,Group_Id,User_Username,GroupPermission_Id的表。

有没有人有任何提示要做到这一点,还是我在考虑错误的设计?

1 个答案:

答案 0 :(得分:3)

在这种情况下,您缺少其他实体。它应该看起来像:

包含PermissionId的新Name实体:

public class Permission
{
    [Key]
    public int Id { get; set; }
    [StringLength(EntityLength.GenericLength)]
    public string Name { get; set; }

    public virtual ICollection<GroupPermission> GroupPermissions { get; set; }
}  

修改GroupPermission实体以在UsersGroupsPermissions之间形成联结表:

public class GroupPermission
{
    [Key]
    public int Id { get; set; }

    public int GroupId { get; set; }
    public virtual Group Group { get; set; }

    public string UserName { get; set; }
    [ForeignKey("UserName")]
    public virtual User User { get; set; }

    public int PermissionId { get; set; }
    public virtual Permission Permission { get; set; }
}