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

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

问题描述

我需要执行一个在数据库中的某个地方保存的customquery,并且我需要它在一个datatable或者数据集中返回,并将它绑定到一个gridview中,这个gridview将自动生成列为true。

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。你会发现它比尝试将数据绑定到一个Entity集合,然后自己填充一个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定制查询,请查看 ExecuteQuery 。它允许您执行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天全站免登陆