EF的导航属性

在EF中,外键被称为导航属性。

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

db.StudentDetails.Include(o=>o.Student).Where(s => s.u_SID == 1).FirstOrDefault().Student.Name;

生成sql语句时,使用Include在sql中内连接inner jion来查询,也就可以查询到外键表Student。

如果是使用select语句,当用到外键的属性时,则会自动生成inner join 语句,也就可以查询到外键。如下面的语句,也可以查询到外键表的数据。

db.StudentDetails.Where(s => s.u_SID == 1).Select(s => new { aId = s.u_SID, aName = s.Student.s_Name }).ToList();

猜你喜欢

转载自www.cnblogs.com/dayang12525/p/10977621.html
EF