简体   繁体   English

如何编辑实体框架生成的SQL查询?

[英]How to edit the sql query that Entity framework generated?

I'm working on Visual studio 2012 (C#) and use Entity Framework to handle a queries with SQL server . 我正在使用Visual studio 2012 (C#)并使用Entity Framework来处理SQL server的查询。

For performance reasons I want to be able to edit the background sql query code and customize it . 出于性能原因,我希望能够编辑后台sql查询代码并对其进行自定义 (to make entity framework more manually and to control what it produce in the back) . (以使实体框架更加手动,并控制其在后面产生的内容)。

I saw all the posts about TraceToString this is not what I mean, I need to be able to edit the background code online and not only show that. 我看到了所有有关TraceToString的帖子,这不是我的意思,我需要能够在线编辑后台代码,而不仅仅是显示它。

EDIT 编辑

if ((activityBox.Text != "") && (biasBox.Text == ""))
{
aggGk = (from sk in db.DIM_COV_TEST_AGGREGATION_KEY 
where activityGk.Contains(sk.COV_ACTIVITY_GK) && (sk.DATE_ID >= beginTime) 
&& (sk.DATE_ID <= endTime) 
select sk.TEST_AGGREGATION_KEY_GK).Distinct().ToList(); 
}

I believe that using if you are not compromised with the performance, you need to follow the TSQL queries. 我相信,如果不影响性能,则需要遵循TSQL查询。 If you are a .NET developer, you will be familiarized with SQL queries. 如果您是.NET开发人员,则将熟悉SQL查询。

The below link gives a greater comparision of different options 下面的链接提供了更多不同选项的比较

  1. Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures? 实体框架VS LINQ to SQL VS ADO.NET与存储过程?

Its is also possible to use your Stored procedures with EF. 也可以在EF中使用您的存储过程。 Check these two links 检查这两个链接

  1. http://msdn.microsoft.com/en-us/data/gg699321.aspx http://msdn.microsoft.com/en-us/data/gg699321.aspx
  2. http://www.codeproject.com/Articles/522611/Entity-Framework-CRUD-Operations-Using-Stored-Proc http://www.codeproject.com/Articles/522611/Entity-Framework-CRUD-Operations-Using-Stored-Proc

We cannot control the execution of EF queries , but surely we can do optimization of SQL queries for better performance. 我们无法控制EF查询的执行,但可以肯定的是我们可以对SQL查询进行优化以提高性能。 All things you need is to generate a model/ entity classes to handle the data. 您需要做的所有事情就是生成一个模型/实体类来处理数据。

您不能(轻松地)影响框架生成的SQL,您正在寻找的可能是实体SQL (可能与ObjectQuery <T>结合使用),它使您可以在实体框架的顶部编写自己的查询。更灵活的方式。

From the top of my head I would say it is not possible. 从我的头顶上,我会说这是不可能的。 The entire point of having a OR framework is to avoid getting into the SQL language. 拥有OR框架的全部目的是避免进入SQL语言。

For more fruitful discussion it would be great if you share what kind of optimization you want to be done by editing the generated SQL alongwith the entities involved. 为了使讨论更加富有成果,如果您共享希望通过编辑所生成的SQL及其所涉及的实体来进行哪种优化,那就太好了。

I would like to Add the following link http://msdn.microsoft.com/en-us/library/bb399560.aspx 我想添加以下链接http://msdn.microsoft.com/en-us/library/bb399560.aspx

This is not exactly editing a SQL generated from the Framework, but you could use this where you need some performance tweaking. 这并不是完全编辑从Framework生成的SQL,但是您可以在需要一些性能调整的地方使用它。

For some specified query that you want to customize, you can try DataContext.ExecuteQuery<TResult>(string query, params object[] parameters) . 对于要自定义的某些指定查询,可以尝试DataContext.ExecuteQuery<TResult>(string query, params object[] parameters) But if you find there's too much you need to customize, it means that it's not a good idea to use Entify Framework in your scenario. 但是,如果您发现需要自定义的内容太多,则意味着在您的方案中使用Entify Framework不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM