Code First Entity Framework创建List<>财产错了

时间:2017-01-10 14:07:14

标签: entity-framework database-design many-to-many entity-framework-core

所以,我有一个代码第一个模型,如下所示:

public class SeciovniContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Permission> Permissions { get; set; }
}
public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }

    [StringLength(30)]
    [Required]
    public string FirstName { get; set; }

    [StringLength(30)]
    [Required]
    public string LastName { get; set; }

    [StringLength(50)]
    [EmailAddress]
    [Required]
    public string Email { get; set; }

    public List<Permission> Permisions { get; set; }
}
public class Permission
{
    [Key]
    // TODO: [Index(IsUnique = true)]
    public string PermissionType { get; set; }

    [Required]
    public string Description { get; set; }
}

而且,在我的种子中,我有这个:

var permissions = new List<Permission> {
    new Permission() { PermissionType = "Admin", Description = "Admin" },
    new Permission() { PermissionType = "Editor", Description = "Editor" }
};

context.Permissions.AddRange(permissions);
context.SaveChanges();

var user1 = new User()
{
    Email = "David@hotmail.com",
    FirstName = "David",
    LastName = "Jones",
    Permisions = permissions,
};

var user2 = new User()
{
    Email = "John@yahoo.com",
    FirstName = "John",
    LastName = "Doe",
    Permisions = permissions.Where(p => p.PermissionType == "Admin").ToList()
};

context.Users.Add(user1);
context.Users.Add(user2);

context.SaveChanges();

我希望这会以某种方式将单个用户与N权限相关联。因此,大卫将拥有权限&#34;管理员&#34;和&#34;编辑&#34;而约翰只会&#34;编辑&#34;。

但是,在创建数据库时,它看起来像这样:

-----------------------------------------
| User Table                            |
-----------------------------------------
| UserID | Email | FirstName | LastName |
-----------------------------------------


-----------------------------------------
| Permissions Table                     |
-----------------------------------------
| PermissionType | Description | UserID |
-----------------------------------------

因此,David拥有Admin权限(但没有编辑),John有编辑权。除此之外,执行db.Users.FirstOrDefault(/* query */).Permissions也是空的。

由于我期待有限数量的权限,我真的期待更像下面的内容,其中有一个&#34;中间&#34;把两者连在一起的表。但我不想手动维护该链接表。有没有办法做我想做的事情:拥有无限数量用户的有限数量的权限(不重复)?

如果这个数据库是手工创建的,那么我还有更多的期望:

-----------------------------------------
| User Table                            |
-----------------------------------------
| UserID | Email | FirstName | LastName |
-----------------------------------------


--------------------------------
| Permissions Table            |
--------------------------------
| PermissionType | Description |
--------------------------------


---------------------------
| UserPermissions Table   |
---------------------------
| PermissionType | UserId |
---------------------------

1 个答案:

答案 0 :(得分:2)

你在问什么被称为与自动联结表的多对多关系。虽然在将来的某个版本中添加了not supported yet in EF Core,但EF6支持它,但plan

所以目前除了维护显式连接实体之外别无选择,如文档中所述。