How to query the "function" only for the "admin" role with Entity Framework

45 views Asked by At

Question: How to query the "function" only for the "admin" role with Entity Framework.

Query result will be a "function" tree structure.

Use: Entity Framework 6

I try to this code, but data's son loaded other role; I try lot way but not work.

var role = _ew.Query<Function>().Where(m => m.Roles.Any(r => r.Title == "admin")).ToList();

This is the models:

public class Function 
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Action { get; set; }
    public string Controller { get; set; }
    public string Icon { get; set; }
    public virtual Function Parent { get; set; }
    public virtual ICollection<Function> Functions { get; set; }
    public int Depth { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Member> Members { get; set; }
    public virtual ICollection<Function> Functions { get; set; }
}
public class Member
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public virtual Role Role { get; set; }

}

This is the data:

        var roles = new List<Role>
        {
            new Role {Title = "admin"},
            new Role {Title = "wubo"}
        };
        context.Roles.AddOrUpdate(m => m.Title, roles.ToArray());
        context.SaveChanges();

        var admin = context.Roles.FirstOrDefault(m => m.Title == "admin");
        var wubo = context.Roles.FirstOrDefault(m => m.Title == "wubo");

        var funcs = new List<Function>
        {
            new Function
            {
                Title = "admin",
                Roles = new List<Role> {admin},
                Functions = new List<Function>
                {
                    new Function
                    {
                        Title = "admin1",
                        Roles = new List<Role> {admin},
                        Functions = new List<Function>
                        {
                            new Function {Title = "admin", Roles = new List<Role> {admin}},
                            new Function {Title = "admin", Roles = new List<Role> {admin}},
                            new Function {Title = "wubo", Roles = new List<Role> {wubo}},
                            new Function {Title = "admin", Roles = new List<Role> {admin}},
                            new Function {Title = "wubo1", Roles = new List<Role> {wubo}},
                        }
                    },
                    new Function {Title = "admin", Roles = new List<Role> {admin}},
                    new Function {Title = "admin", Roles = new List<Role> {admin}},
                    new Function {Title = "wubo6", Roles = new List<Role> {wubo}},
                    new Function {Title = "admin", Roles = new List<Role> {admin}},
                    new Function {Title = "admin", Roles = new List<Role> {admin}},
                }
            },
            new Function {Title = "admin", Roles = new List<Role> {admin}},
            new Function {Title = "admin", Roles = new List<Role> {admin}},
            new Function {Title = "admin", Roles = new List<Role> {admin}},
            new Function {Title = "wubo2", Roles = new List<Role> {wubo}},
            new Function {Title = "admin", Roles = new List<Role> {admin}},
            new Function {Title = "wubo3", Roles = new List<Role> {wubo}},
            new Function {Title = "wubo4", Roles = new List<Role> {wubo}},
            new Function {Title = "wubo5", Roles = new List<Role> {wubo}},
            new Function {Title = "admin", Roles = new List<Role> {admin}}
        };
        context.Functions.AddOrUpdate(m => m.Title, funcs.ToArray());
        context.SaveChanges();

        var member = new List<Member>
        {
            new Member {UserName = "admin", Password = "000", Role = admin},
            new Member {UserName = "wubo", Password = "000", Role = wubo}
        };
        context.Members.AddOrUpdate(m => m.UserName, member.ToArray());
        context.SaveChanges();
1

There are 1 answers

1
JamieD77 On

Sounds like you just need to use the Role

var funcs = _ew.Query<Role>().Where(m => m.Title == "admin").Select(a => a.Functions).ToList();

or something close to that