[英]Entity Framework Querying with Navigate Objects
i have entity classes below,i want to a query like sql,how is that posssible with entity framework?? 我下面有实体类,我想查询类似sql的查询,实体框架怎么可能?
select * from SiteUsers su
inner join SiteUserRoles sur on su.Id=sur.SiteUserId
inner join SiteRoleActions sra on sur.SiteRoleId = sra.SiteRoleId
inner join SiteActions sa on sa.Id = sra.SiteActionId
where su.Id=1 and sa.ParentId=189
public class SiteRole
{
public SiteRole()
{
this.SiteActions = new HashSet<SiteAction>();
this.SiteUser = new HashSet<SiteUser>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<SiteAction> SiteActions { get; set; }
public virtual ICollection<SiteUser> SiteUser { get; set; }
}
public partial class SiteUser
{
public SiteUser()
{
this.SiteRoles = new HashSet<SiteRole>();
}
public int Id { get; set; }
public string Email { get; set; }
public string UserPassword { get; set; }
public string UserName { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public virtual ICollection<SiteRole> SiteRoles { get; set; }
}
public partial class SiteAction
{
public SiteAction()
{
this.Childs = new HashSet<SiteAction>();
this.SiteRoles = new HashSet<SiteRole>();
}
public int Id { get; set; }
public string Name { get; set; }
public string ImagePath { get; set; }
public string ActionName { get; set; }
public string ControllerName { get; set; }
public Nullable<int> ParentId { get; set; }
public Nullable<int> Type { get; set; }
public virtual ICollection<SiteAction> Childs { get; set; }
public virtual SiteAction Parent { get; set; }
public virtual ICollection<SiteRole> SiteRoles { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SiteUser>().HasMany<SiteRole>(r => r.SiteRoles).WithMany(u => u.SiteUser).Map(m =>
{
m.ToTable("SiteUserRoles");
m.MapLeftKey("SiteUserId");
m.MapRightKey("SiteRoleId");
});
modelBuilder.Entity<SiteRole>().HasMany<SiteAction>(r => r.SiteActions).WithMany(u => u.SiteRoles).Map(m =>
{
m.ToTable("SiteRoleActions");
m.MapLeftKey("SiteRoleId");
m.MapRightKey("SiteActionId");
});
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
Since you don't have SiteUserRoles and SiteRoleActions as entities but only as join tables (if I read you well), you should use the navigation properties in stead of joining. 由于您没有将SiteUserRoles和SiteRoleActions作为实体,而仅将它们作为联接表(如果我读得很好),因此应该使用导航属性代替联接。 Something like:
就像是:
conext.SiteUsers.Where(su => su.id == 1
&& su.SiteRoles.SelectMany(sr => sr.SiteActions)
.Any(sa => sa.ParentId == 189 )
(Not syntax checked). (未检查语法)。
var result= (from su in SiteUsers
join sur in SiteUserRoles
on su.Id=sur.SiteUserId
join sra in SiteRoleActions
on sur.SiteRoleId = sra.SiteRoleId
join sa in SiteActions
on sra.SiteActionId=sa.Id
where su.Id=1 and sa.ParentId=189
select su).ToList();
Hope this will help . 希望这会有所帮助。 result will be a list of type SiteUsers.
结果将是SiteUsers类型的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.