使用原始SQL查询在Dapper和Entity Framework中如何处理对象之间有什么区别? [英] What is the difference between how objects are handled in Dapper and Entity Framework - with raw SQL queries?

查看:82
本文介绍了使用原始SQL查询在Dapper和Entity Framework中如何处理对象之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的阅读,Dapper可能对SQL更友好,但是Entity Framework(EF)允许使用原始SQL。我通常使用SQL,但我喜欢EF中的某些功能。我的许多查询都相当复杂,涉及多个数据库和数据库引擎,并且大多数查询不只是简单的CRUD。

From my reading, Dapper is suppose to be more SQL friendly, but Entity Framework (EF) allows raw SQL. I typically use SQL, but I like some of the things in EF. Many of my queries are fairly complex, cover more than one database and database engine, and most are not just simple CRUD.

使用SQL时,物体处理方式的差异?当我发出查询时,如果返回某些内容并将其分配给< dynamic> List 类型,它们都相同吗?

When it comes to using SQL, what is the difference in how an object is handled? When I issue a query, if something comes back and I assign it to a <dynamic> List type, are they both the same?

我正在使用ASP.NET Core 1.x。

I am using ASP.NET Core 1.x.

推荐答案

如果您使用EF发出SQL请求,那么您实际上并没有使用EF。调用是一行一行的等效于ADO,并且定义和维护的结果对象的加载是微不足道的。很多人都使用EF,但是在他们想做原始SQL的时候使用Dapper。 Dapper缓存结果集到POCO的映射,因此后续查询运行得更快。我不认为EF会这样做。此外,在Dapper中,参数处理要好得多。

If you're using EF to issue SQL requests, you're not really using EF. The calls are line for line equivalent to ADO, and the loading of the result object, which you get to define and maintain, is trivial. Lots of folk use EF, but then use Dapper for the times they want to do raw SQL. Dapper caches the mapping of the result-set to POCO, so subsequent queries run faster. I don't think EF does this. Also the parameter handling is much nicer in Dapper.

但是您仍然对字符串字面量的SQL感到困惑,每个错误都是运行时错误并且您必须编写和维护结果POCO。我认为,如果您使用 QueryFirst (免责声明:我写的)。您的SQL位于.sql文件中,语法在您键入时经过验证。在设计时根据查询结果生成的POCO。您的查询一直在经过集成测试,并且 许多其他好处 (我将在本文中进行记录)很快!

But you're still stuck with SQL in string literals, every error is a runtime error and you have to write and maintain your result POCOs. I contend you'll be happier and live longer if you use QueryFirst (disclaimer: which I wrote). Your SQL in a .sql file, syntax validated as you type. Your POCOs generated at design time from your query results. Your queries continually integration tested, and numerous other benefits which I'll be documenting shortly!

这篇关于使用原始SQL查询在Dapper和Entity Framework中如何处理对象之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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