使用实体框架执行自定义 sql? [英] execute custom sql with entity framework?

查看:26
本文介绍了使用实体框架执行自定义 sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行一个自定义查询,该查询将保存在数据库中的某个位置,我需要它返回一个数据表或数据集,并将其绑定到一个网格视图,该网格视图将自动生成列为真.

I need to execute a customquery which wil be saved somewhere in the database and I need it to return in a datatable, or dataset and bind it to a gridview which will have autogenerate columns to true.

我所有的数据访问层都可以与实体框架完美配合,但对于某些特定场景,我需要这样做,我想知道是否应该将 ado.net 与实体框架结合起来,或者 EF 是否可以以某种方式做到这一点

All my Data access layer works perfect with entity framework, but for some specific scenario I need to do this and I wonder if I should combine ado.net with entity framework, or if EF can do it somehow

推荐答案

如果您的目标是返回 ADO.NET 结构(DataTable 或 DataSet),那么只需使用经典的 ADO.NET.您会发现这比尝试将数据绑定到实体集然后自己填充 DataTable 或 DataSet 更容易.

If your goal is to return ADO.NET structures (DataTable or DataSet), then just use classic ADO.NET. You'll find it easier than trying to bind data to an Entity set and then populating a DataTable or DataSet yourself.

但是,如果您真的有兴趣通过 EntityFramework 运行自定义查询,请查看 执行查询.它允许您执行 SQL 查询并将结果映射回模型中的实体.然后,您可以练习获取 IEnumerable 结果并将其映射到 DataTable 或 DataSet.因此,我最初的回答是只需使用良好的老式 ADO.NET 方法即可."

However, if you're really and truly interested running a custom query through EntityFramework, take a look at ExecuteQuery. It allows you to execute a SQL Query and maps the result back to entities in your model. It would then be an exercise on your part to take the IEnumerable result and map it to a DataTable or DataSet. Hence my original answer of "just do it with good ol' fashioned ADO.NET methods."

这篇关于使用实体框架执行自定义 sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆