简体   繁体   中英

how to show values of three table with foreign key associated in table using entity framework c#?

hello all i am new to entity and asp.net MVC i am using entity framework code first approach with already made database i want to show three tables data in a table using inner join and confused how to do it

i have tried the following code given below

 public List<NewGroupRoles> GetAllGroups()
    {

        try
        {
            using(var p=new VMSDBContext())
            {
                var group = (from group_role in p.group_roles
                             join groups in p.groups on group_role.group_id equals groups.group_id
                             join roles in p.roles on group_role.roles_id equals roles.role_id
                             select new
                             {
                                 Id = group_role.group_roles_id,
                                 group_name = groups.group_name,
                                 group_role = roles.role_name
                             }).ToList();


            }

        }
        catch(Exception ex)
        {
            return new List<NewGroupRoles>();
        }
    }

i want to return it from a function in model

model classes are ths class defines all the database entity classes

this a group table

[this is group role class ][3]

role class

You are trying to do work, which EF is supposed to do for you.

It looks like you have many-to-many relationship between the groups and roles tables. Why wouldn't you remove the group_roles class and just define navigation properties

public virtual IEnumerable<roles> roles { get; set; }

in groups model class and

public virtual IEnumerable<groups> groups { get; set; }

in roles model class

and get list of groups and roles somehow like this:

var groupRoles = p.groups.SelectMany(g=>g.roles, (g,r)=>new {Group = g.group_name, Role = r.role_name}).ToList();

Or if your use EF Core, which yet does not support many-to-many relationships without intermediate model class or just insist on having an intermediate class group_roles in your model, you can define two navigation properties in it, something like this:

public class group_roles
{
    [Key]
    public int group_roles_id { get; set; }

    [ForeignKey("groups")]
    public int group_id { get; set; }

    [ForeignKey("roles")]
    public int role_id { get; set; }

    public virtual groups group { get; set; }
    public virtual roles role { get; set; }
}

Then your query is just

var groupRoles = p.group_roles.Select(gr=> new {gr.group_roles_id, Group=gr.group.group_name, Role=gr.role.role_name).ToList();

Use this as you need to have new object while selecting

public List<NewGroupRoles> GetAllGroups()
            {

                try
                {
                    using(var p=new VMSDBContext())
                    {
                        var group = (from group_role in p.group_roles
                                     join groups in p.groups on group_role.group_id equals groups.group_id
                                     join roles in p.roles on group_role.roles_id equals roles.role_id
                                     select new NewGroupRoles()
                                     {
                                         Id = group_role.group_roles_id,
                                         group_name = groups.group_name,
                                         group_role = roles.role_name
                                     }).ToList();


                    }

                }
                catch(Exception ex)
                {
                    return new List<NewGroupRoles>();
                }
       return group;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM