ef core 使用include进行外键连接查询

在entity framework core中,如果两个实体涉及到外键连接,查询的时候默认是只查自身而不会去查询外键表的。如果想要让查询结果包含外键实体,则需要使用include方法来让查询结果包含外键实体。如

_dbContext.ProductStandard.Include(o=>o.Product).SingleOrDefault(o => o.Id == id);

这个例子中,Product的Id和 ProductStandard的ProductId字段进行外键连接,想要让查询出来的productStandard携带 Product,则必须使用Include方法来进行声明。

posted on 2018-07-09 15:17  axel10  阅读(9966)  评论(1编辑  收藏  举报