How to obtain ready-to-execute queries? - entity-framework

Is it possible to obtain SQL queries with inlined parameters of EF context?
I would like to be able to copy-paste queries into management studio and execute them.
I work with ctx.Database.Log delegate, but it traces queries with parameters' values placed inside comments, eg:
.... WHERE [Extent1].[Name] = #p__linq__0
-- p__linq__0: 'ApproveReminder' (Type = AnsiString, Size = 8000)
Which are not runnable without first editing them.

If I understand your question properly, you can do this with SQL Server Profiler.
Run it, and then set up a profile watching the server that EF is connecting to. While it's running, execute your code so that the EF query you're interested in runs against the server. Then stop the profile.
If you peruse what it's recorded, you should be able to identify your query quite easily. Then click on it and the full query text will appear in the window at the bottom of the screen.

You could use Hibernating Rhinos Entity Framework Profiler (commercial software) or you could do this:
var query = context.Customers.Where(...);
((System.Data.Objects.ObjectQuery) query).ToTraceString();
http://visualstudiomagazine.com/blogs/tool-tracker/2011/11/seeing-the-sql.aspx

Related

Can I debug a PostgreSQL query sent from an external source, that I can't edit?

I see how to debug queries stored as Functions in the database. But my problem is with an external QGIS plugin that connects to my Postgres 10.4 via network and does a complex query and calculations, and stores the results back into PostGIS tables:
FOR r IN c LOOP
SELECT
(1 - ST_LineLocatePoint(path.geom, ST_Intersection(r.geom, path.geom))) * ST_Length(path.geom)
INTO
station
(continues ...)
When it errors, it just returns that line number as the failing location, but no clue where it was in the loop through hundreds of features. (And any features it has processed are not stored to the output tables when it fails.) I totally don't know enough about the plugin and about SQL to hack the external query, and I suspect if it was a reasonable task the plugin author would have included more revealing debug messages.
So is there some way I could use pgAdmin4 (or anything) from the server side to watch the query process? Even being able to see if it fails the first time through the loop or later would help immensely. Knowing the loop count at failure would point me to the exact problem feature. Being able to see "station" or "r.geom" would make it even easier.
Perfectly fine if the process is miserably slow or interferes with other queries, I'm the only user on this server.
This is not actually a way to watch the RiverGIS query in action, but it is the best I have found. It extracts the failing ST_Intersects() call from the RiverGIS code and runs it under your control, where you can display any clues you want.
When you're totally mystified where the RiverGIS problem might be, run this SQL query:
SELECT
xs."XsecID" AS "XsecID",
xs."ReachID" AS "ReachID",
xs."Station" AS "Station",
xs."RiverCode" AS "RiverCode",
xs."ReachCode" AS "ReachCode",
ST_Intersection(xs.geom, riv.geom) AS "Fraction"
FROM
"<your project name>"."StreamCenterlines" AS riv,
"<your project name>"."XSCutLines" AS xs
WHERE
ST_Intersects(xs.geom, riv.geom)
ORDER BY xs."ReachID" ASC, xs."Station" DESC
Obviously replace <your project name> with the QGIS project name.
Also works for the BankLines step if you replace "StreamCenterlines" with "BankLines". Probably could be adapted to other situations where ST_Intersects() fails without a clue.
You'll get a listing with shorter geometry strings for good cross sections and double-length strings for bad ones. Probably need to widen your display column a lot to see this.
Works for me in pgAdmn4, or in QGIS3 -> Database -> DB Manager -> (click the wrench icon). You could select only bad lines, but I find the background info helpful.

Get Error when Save modifed record using Light Switch on Azure

I am using Light switch on Azure.
After I modified a column in a record when I click the Save button I got
"Store update, insert, or delete statement affected an unexpected number of rows(0). Entties may have been modified or deleted since entities were loaded, Refresh ObjectStateManager entries.
I use VS 2012 on my dev machine debug this light switch app. it works fine and no errors when I modify the save column on same records then save it.
Is anybody in this forum has idea what could cause this? and how should I work around it?
I suspect the azure machine don't have the same version of EF with my dev machine. but in the Light switch project both client and server reference I could not find the EF is referenced there. So I don't know how I can bring the EF dll on my machine up to Azure machine.
Anybody could give me some suggestion on this?
Thanks
Chris
Usually it's a side effect of Optimistic Concurrency. This article can give you the idea of it in Lightswitch:
LightSwitch 2012 Concurrency Enhancements
When it's working on dev machine and it's not working on Azure, I guess something is not right in your production database.
you can also take a look at Entity framework: affected an unexpected number of rows(0)
Having Instead of insert/update triggers, sometimes SQL server does not report back an IdentityScope for each new inserted/updated row. Therefore EF can not realize the number of affected rows.
Normally, any insert/update into a table with identity column are immediately followed by a select of the scope_identity() to populate the associated value in the Entity Framework. The instead of trigger causes this second step to be missed, which leads to the 0 rows inserted error.
You can change your trigger to be either before or after insert or tweak your trigger by adding following line at the end of it:
select [Id] from [dbo].[TableXXX] where ##ROWCOUNT > 0 and [Id] = scope_identity()
Find more details in this or this thread.

How can I see all the SQL queries that are being ran on each page load?

Django has a debug toolbar that lets me see all the queries being ran, how can I view them in MVC4?
If you want to see just executed SQL commands you can use IntelliTrace (only available in VS Ultimate edition). If you want to have per request profiling you can try MVC Mini Profiler. EF doesn't have any built-in tool for tracking executed SQL commands.
There are quite a number of tools that you can consider. I discuss a number of these at http://www.thinqlinq.com/Post.aspx/Title/LINQ-to-Database-Performance-hints including Intellitrace, and SQL Profiler, and other relatively inexpensive profiling tools out there like the MVC MiniProfiler, ORM Profiler, Huagati’s LINQ to SQL Profiler, EF Prof, or at a bare minimum checking the generated code for your queries using LinqPad. Some of these options require you to modify your existing code base to plug in the profiler. Others just intercept the traffic to the database. It doesn't really matter which one you use as long as you use something particularly while you are learning.
Oks, let us take an example of northwind database
using(NorthwindEntities context = new NorthwindEntities())
{
var query = from p in context.Products
where p.Product_ID == 3
select p;
//Query can be traced like this
var SqlQuery = (System.Data.Objects.ObjectQuery<Product>)query;
Console.WriteLine(SqlQuery.ToTraceString());
}
you can use this var SqlQuery = (System.Data.Objects.ObjectQuery<Product>)query; at your page load and can print the value wherever you want.

T-SQL Hierarchy to duplicate Dependent Objects tree view in SQL Server 2005

Id like to map the calling stack from one master stored procedure through its hundreds of siblings. i can see it in the dialog, but cannot copy or print it, but couldnt trap anythiing worthwhile in proflier.
do you know what sproc fills that treeview? i must be a recursive CTE that reads syscomments or information_schema.routines, but its beyond my chops, though i can imagine it
thanks in advance
drew
you might want to look at the source code for sp_depends (Transact-SQL).
In SQL Server Management Studio:
go to the "master" database
and then "Programmability"
then "Stored Procedures"
then "System Stored Procedures"
then "sys.sp_depends"
In sp_depends's code, there are queries on the tables you'd need to hit to build output like you are after.

SQl Developer Single Query result tab please

In the free application SQL-Developer (provided by Oracle), I tried searching around for this but couldn't immediately find a solution. I find the opening of multiple SQL query result window/tabs mildly annoying. I'm sure there are very useful cases for this feature, but my question is:
Can we turn the multiple query result windows to just one (Toad style). If there's a shortcut key for this, that would be super awesome.
SQL Developer 4.0 (2013)
Close all the Query Result tabs
Tools > Preferences
Database > Worksheet > uncheck "Show query results in new tabs"
So, it turns out this is a bug. If you look closely, you'll notice that for each query run, your results tab is automatically being pinned, causing each new query run to need it's own new tab to display the results.
The automatic pinning is only supposed to be enabled when you explicitly go into Tools->Preferences->Database->Worksheet->"Automatically Freeze Result Tabs".
It is an identified bug, that Oracle fixed with SQL Developer 2.1.1.
Go to oracle.com and download the latest version and this should go away. It was definitely very annoying.
Instead of "Run Statement" click on "Run Script"(F5). This would give the output of two queries in the same Query Result window.
Directly to the database or from an application? Do you mean returning one result set from multiple tables together or multiple result sets in one pane? Are you using SQL 2000, 2005, 2008, or something else? The question is vaguely worded, but I'll try to help anyway.
For the purposes of this answer, I think you're trying to query the database directly. Open SQL Server Management Studio 2005 or newer (Not sure if this works in 2000), click New Query, and type the multiple queries into the pane. i.e.:
select * from table1
select * from table2
will return two result sets in the same window/pane.
Unpin helps keep current tab for next query.
(I am using Oracle SQL Developer 22.x)
I found that this was happening in SQL Dev v4.0.2.15 because I was running 2 SELECT statements. Running one SELECT at a time re-used the Results tab.