[英]Entity Framework 7 Lambda expression for 3 table joins
I am trying to built an API that will return all the data for a server. 我正在尝试构建一个API,该API将返回服务器的所有数据。 A server can have multiple endpoints and they are stored in separate table with many-to-many relationship.
一个服务器可以有多个端点,并且它们以多对多关系存储在单独的表中。
I have 3 tables: 我有3张桌子:
Servers
table contains all the server details Servers
表包含所有服务器详细信息 Applications
table that contains application names Applications
表 application_endpoint
table that contains foreign keys to both tables Servers
and Applications
application_endpoint
表,其中包含指向表Servers
和Applications
外键 Here is my database model: 这是我的数据库模型:
Here are my models: 这是我的模型:
public class Servers
{
public int id { get; set; }
public string server_name { get; set; }
public string alias { get; set; }
public string ip_address { get; set; }
public Status status { get; set; }
public virtual ICollection<Application_endpoints> endpoint { get; set; }
}
public class Application_endpoints
{
public int id { get; set; }
public int? server_id { get; set; }
[ForeignKey("server_id")]
public Servers server { get; set; }
public int? application_id { get; set; }
[ForeignKey("application_id")]
public Applications application { get; set; }
}
public class Applications
{
public int id { get; set; }
public string app_name { get; set; }
}
public class ServerDbContext : DbContext
{
public DbSet<Applications> Applications { get; set; }
public DbSet<Application_endpoints> Application_endpoints { get; set; }
public DbSet<Servers> Servers { get; set; }
}
In my Api Controller, I have created HttpGet
method that will query database and return data for each server. 在我的Api Controller中,我创建了
HttpGet
方法,该方法将查询数据库并为每个服务器返回数据。 Here is simple API for GET: 这是用于GET的简单API:
private ServerDbContext _context;
public ServersController (ServerDbContext context)
{
_context = context;
}
// GET: api/values
[HttpGet]
public JsonResult Get()
{
var servers = _context.Servers
.Include(endpoint => endpoint.endpoint)
.ToList();
return Json(servers);
}
Now when I make a get request, I get data back from the database, however the application
object in below JSON is returned null. 现在,当我发出get请求时,我从数据库中获取了数据,但是JSON下面的
application
对象返回null。 I am trying to figure it out how to add left join in my above query so I can get application name from applications table. 我试图弄清楚如何在我上面的查询中添加左联接,以便我可以从应用程序表中获取应用程序名称。
{
"id": 6,
"server_name": "server1",
"alias": "",
"ip_address": "192.168.1.7",
"endpoint": [
{
"id": 23,
"server_id": 6,
"application_id": 10,
"application": null
}
]
}
Any help is really appreciated. 任何帮助都非常感谢。 :)
:)
To all, 所有人
Thank you for guiding me to right path. 感谢您引导我走上正确的道路。 I did quick google search and it appears that syntax has changed in EF7.
我做了快速的谷歌搜索,似乎语法已在EF7中更改。 Below worked for me :)
下面为我工作:)
var servers = _context.Servers
.Include(e => e.endpoint).ThenInclude(a => a.application)
.ToList();
You need to change your include in your query: 您需要更改查询中的包含内容:
from 从
.Include(endpoint => endpoint.endpoint)
to 至
.Include(s => s.endpoint.Select(e => e.application))
Or you can use string path as 或者您可以使用字符串路径作为
.Include("endpoint.application")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.