i have entity classes below,i want to a query like sql,how is that posssible with entity framework??
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.