简体   繁体   English

如何使用实体框架c#显示与表中关联的外键的三个表的值?

[英]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 您好所有我对实体和asp.net MVC都是陌生的我正在使用实体框架代码优先方法与已经建立的数据库,我想使用内部联接在一个表中显示三个表数据,并且困惑该怎么做

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] [这是小组角色类] [3]

role class 角色类

You are trying to do work, which EF is supposed to do for you. 您正在尝试做EF应该为您做的工作。

It looks like you have many-to-many relationship between the groups and roles tables. 看起来您在groupsroles表之间具有多对多关系。 Why wouldn't you remove the group_roles class and just define navigation properties 为什么不删除group_roles类并只定义导航属性

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

in groups model class and groups模型课上

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

in roles model class roles模型类

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: 或者,如果您使用的EF Core在没有中间模型类的情况下仍不支持多对多关系,或者仅坚持在模型中具有中间类group_roles ,则可以在其中定义两个导航属性,如下所示:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该将外键表存储为Entity Framework中的外键ID还是该表的C#模型? - Should I store foreign key table as foreign key ID or the C# model of that table in Entity Framework? 如何使用Postgresql将值添加到2个表,然后将外键从一个表映射到Entity Framework Core中的另一个表? - How to add values to 2 table then map foreign key from one table to another table in Entity Framework Core using Postgresql? 如何将第一个表的主键值更新为第二个表的外键(实体框架代码第一个模型)? - How to update primary key values of first table into foreign keys in the second table (Entity framework code first model)? 实体框架C#在表Y中引入外键X可能会导致循环或多个级联路径 - Entity Framework C# Introducing A Foreign Key X In Table Y May Cause Cycles OR Multiple Cascade Paths 使用具有自动迁移实体框架的新表添加外键mvc c# - Adding foreign key with new table with automatic migrations entity framework mvc c# 使用C#将外键插入表中 - Inserting a Foreign Key into a table using C# 如何在ASP.NET MVC中使用实体框架在父表中创建外键并将其分配给子表 - How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc 使用实体框架使用外键将不同的记录插入表中 - Insert distinct record into table with foreign key using Entity Framework 使用实体框架检索逗号分隔的外键表数据 - Retrieve comma separated foreign key table data using Entity Framework 如何在C#中为表设置外键? - How to set a foreign key for a table in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM