【发布时间】:2011-08-23 16:27:09
【问题描述】:
我对使用实体框架还是很陌生,我很难理解如何编写使用多对多关系的查询。我有 3 个实体。 角色、用户和安全。一个Role可以有多个Securables,一个Securable可以分配给多个Roles。一个角色可以有多个用户,一个用户可以有多个角色。
我的问题是:我将如何编写一个查询,为给定的用户 ID 提供不同的 Securables 列表?
这是我的模型,EF 自动为我创建链接表。
public class SecurityContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Securable> Securables { get; set; }
}
public class User
{
public Guid UserId { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
public class Securable
{
public Guid SecurableId { get; set; }
public string Name { get; set; }
public virtual ICollection<Role> Roles { get;set;}
}
public class Role
{
public Guid RoleId { get; set; }
public string Name { get; set; }
public virtual ICollection<Securable> Securables { get; set; }
public virtual ICollection<User> Users { get; set; }
}
【问题讨论】:
标签: c# .net entity-framework entity-framework-4.1