[英]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 模型类是定义所有数据库实体类的类
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. 看起来您在
groups
和roles
表之间具有多对多关系。 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.