Where to find translated Linq to Entity query to Sql

后端 未结 3 845
臣服心动
臣服心动 2021-01-18 01:30

I want to get the translated Linq query programmatically and do some stuff with that Sql syntax.

Suppose this is my code:

public class MyApiControlle         


        
相关标签:
3条回答
  • 2021-01-18 02:01

    I think these posts may help you:

    1) How to view LINQ Generated SQL statements?

    2) How do I view the SQL generated by the Entity Framework?

    Couple of examples (from the above):

    var sql = ((System.Data.Objects.ObjectQuery)objs).ToTraceString();
    var sql = objs.ToString();
    

    Documentation on .ToTraceString() can be found here:

    MSDN trace string documentation

    0 讨论(0)
  • 2021-01-18 02:09

    another option if you are using Entity Framework 6 is use the new feature to logging whats is happen, you can get the t-sql and the query time:

    Logging and Intercepting Database Operations

    using (var context = new BlogContext()) 
    { 
        context.Database.Log = Console.Write; //here, you can write this info to a text file for example.
    
        // Your code here... 
    }
    
    0 讨论(0)
  • 2021-01-18 02:17

    You can call the ToString() method on objs. This will result in a call to the ToTraceString method that returns the executed SQL:

    string sql = objs.ToString();
    
    0 讨论(0)
提交回复
热议问题