ASP.NET Core EF 查询获取导航属性值,使用Include封装
2018-11-05 15:54
1121 查看
// 引用 using Microsoft.EntityFrameworkCore; // 摘要: // Specifies related entities to include in the query results. The navigation property // to be included is specified starting with the type of entity being queried (TEntity). // Further navigation properties to be included can be appended, separated by the // '.' character. // // 参数: // source: // The source query. // // navigationPropertyPath: // A string of '.' separated navigation property names to be included. // // 类型参数: // TEntity: // The type of entity being queried. // // 返回结果: // A new query with the related data included. public static IQueryable<TEntity> Include<TEntity>([NotNullAttribute] this IQueryable<TEntity> source, [NotNullAttribute][NotParameterized] string navigationPropertyPath) where TEntity : class;
core中提供的扩展方法Include有两个重载方法,我们这里使用第一个重载方法,传参数导航属性名字,返回IQueryable<TEntity>,多对多导航属性,二级导航属性需要用‘.’点分隔符连接,提供完整导航属性名称。
下面是我封装的扩展方法:
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; namespace System { public static class IQueryableExtensions { /// <summary> /// 导航属性,参数:导航属性名称字符串,支持多表查询 /// 多级导航属性:“属性名.属性名” 用‘.’连接 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="Properts"></param> /// <returns></returns> public static IQueryable<T> In<T>(this IQueryable<T> obj, params string[] Properts) where T : class { IQueryable<T> data = obj; foreach (var prop in Properts) { data = data.Include(prop); } return data; } } }View Code
public class FREEFUNC { [Key] public long FFID { get; set; } public Nullable<int> SCID { get; set; } [ForeignKey("FUNCDEFINE")] public int FID { get; set; } public virtual FUNCDEFINE FUNCDEFINE { get; set; } } public partial class FUNCDEFINE { [Key] public int FID { get; set; } public string FNAME { get; set; } public int CANOPR { get; set; } public int ISMENU { get; set; } public int ISEDIT { get; set; } public Nullable<int> FUNCTYPE { get; set; } [ForeignKey("FUNCDEFINE")] public Nullable<int> HIGHFID { get; set; } public string ICON { get; set; } public string CONTROLLER { get; set; } public string ACTION { get; set; } public virtual FUNCDEFINE HIGHF { get; set; } }
调用实例:
var list = FREEFUNCService.GetList().In("FUNCDEFINE", "FUNCDEFINE.HIGHF").ToList();
注:GetList()返回IQueryable<FREEFUNC>类型,IEnumerable<T>类型不支持Include方法,导航属性必须延迟查询时调用,最终生成连表查询sql语句。
另外不使用Include方法也可以获取导航属性,获得IQueryable对象延迟查询,再使用.Select查询时返回值中获取导航属性值,最终也会生成连表查询,foreach不支持。
.net core ef中 获取数据直接ToList() 导航属性为null。
相关文章推荐
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- 使用Asp.Net Core MVC 开发项目实践[第三篇:基于EF Core的扩展]
- Angular5,使用EF和WEB API进行库存管理的ASP.NET Core CRUD
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- 【ASP.NET Core】EF Core - “导航属性”
- asp.net Core使用EF Core自动生成博客/帖子的创建日期
- asp.net core使用jexus部署在linux无法正确 获取远程ip的解决办法
- Asp.net Core中使用NLog,并封装成公共的日志方法
- asp.net core中使用EF Core自动生成表的Id主键
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例
- 使用Asp.Net Core MVC 开发项目实践[第四篇:基于EF Core的扩展2]
- Asp.net Core 使用EF连接MariaDb
- Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例
- 使用Asp.Net Core MVC 开发项目实践[第二篇:EF Core]
- ASP.NET MVC & EF 构建智能查询 一、智能查询的需求与设计
- 操作必须使用一个可更新的查询(ASP.Net)
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 在ASP.NET中重写URL 方法一:使用Request.PathInfo 参数而不是查询字符串