Linq to Sql. How to log SQL queries? - c#

In some point I have an instance of System.Data.Linq.DataContext. Here is the code:
using (System.Data.Linq.DataContext ctx = GetContext(...))
{
return ctx.RoamingCountries?.ToList();
}
I would like to know which SQL query I will have generated by ORM.
I saw this way in the internet:
using (System.Data.Linq.DataContext ctx = GetContext(...))
{
ctx.Log = Console.Out;
return ctx.RoamingCountries?.ToList();
}
But I completely don't understand where I can see the result! I'm working on Windows 10 and Visual Studio 19. All debug windows keep silence.
Sorry if the question is silly. I haven't worked with .NET Framework 4.8 and Visual Studio before. Usually it's .NET Core, EF and Rider. With this stack a
queries logging is simple: add particular middleware and look at the console. But now with Linq to SQL and VS I can't figure out what I should to do to see my SQL queries.

Related

Entity Framework Display SQL Used to Perform DB Request Anomaly

Very strange situation. I am using EF 6.1 in a project along with my friend. He told that when he sets a breakpoint on return from this method
private static List<Tag> PreloadTags(ref ApplicationDbContext db)
{
var enumerableResult = db.Tags.OrderBy(x => Guid.NewGuid()).Take(PrefetchSize*TagsPerPicture);
return enumerableResult.ToList();
}
and howevers over the enumerableResult he sees the actual SQL query used to query the SQL server. However (and this is the anomaly), when I do this, I do not see the query. I only see the type System.Data.Entity.Infrastructure.DbQuery. I am using VS2013 Professional, he is using VS2013 Web Express. Any ideas? It is a great mystery to me although I am sure there is reasonable explanation.
Screenshot of him seeing the query.
Screenshot of me NOT seeing the query.

Linq to SQL for WebMatrix (Web Pages)

I'm wondering if I can use Linq to SQL in my webmatrix-based sites (C#/Razor etc). I love database searching and stuff and it would be awesome if I could use Linq to SQL for that - or at the least, have rows returned, as a dataset or something and then linq over that. Is this possible with WebMatrix-based sites?
I haven't seen anything about Linq in the docs for WebMatrix.
You can use Linq To SQL or the Entity Framework with Web Pages sites. I would recommend using Visual Studio 2010 SP1 or Visual Web Developer 2010 SP1 as they provide the tooling support for Linq To SQL and Entity Framework. I've written a couple of articles on Entity Framework with Web Pages:
http://www.mikesdotnetting.com/Article/185/Building-A-Razor-Web-Pages-Site-With-Database-First-And-Entity-Framework
http://www.mikesdotnetting.com/Article/182/Entity-Framework-Code-First-Development-With-WebMatrix
Entity Framework is the ORM tool from Microsoft that's receiving all the development attention. While LINQ to SQL is not exactly "dead", it doesn't get as much love. Of the two, EF is the way to go, in my opinion.
Webmatrix provides it's own integrated data tools, these tools are not Linq based.
Nothing prevents you from using Linq as far as I know, but you have to eschew the built-in tools to do so. You won't find a data designer, for instance, that works with L2S or EF. You could use Code first though.
It's easy just add linq in the top:
#using System.Linq;
And after that your query:
var selectedDates = db.Query("Select CompleteDate, DueDate FROM Records WHERE Id=#0", ID);
var Date = selectedDates.Select(s => s.CompleteDate).ToArray();
And that its all =D enjoy

Why dont any queries get run when using linq after upgrading to nhibernate 3.0

i was using nhibernate 2.1 and the linq provider. i had the following code:
public IQueryable<T> Query<T>()
{
return Session.Linq<T>();
}
which would allow me to run queries like this:
IEnumerable<Picture> pictures = Query<Picture>();
i just upgraded to nhibernate 3.0 (which seems to have more mature linq functionality) and this code
Session.Linq<T>
doesn't compile
i tried replacing it with:
Session.Query<T>
which compiled but all my queries now return 0 records.
can anyone help explain to me the upgrade path for this and if i am doing something wrong? based on this post, this syntax should work but always seems to return 0 records.
NOTE:
Some additional info: I just ran nhibernate profiler and it detects the session but it doesn't show any SQL queries being run. Is there any reason that anyone can think of that would not have this run any query??
I figured it out. The issue was a post build event that was running a script to copy the old version of nhibernate back to the bin directory
I would not expect IEnumerable pictures = Session.Query(); to execute some sql, because an enumerable is declared, but not executed. IEnumerable pictures = Session.Query().ToList(); will execute sql.

View EF4 generated queries?

LINQ-to-SQL had several ways, including a visualizer add-in, to view the generated SQL from an IQueryable.
I can't find the equivalent for Entity Framework 4. Nothing on StackOverflow, no blogs. How is it done?
Preferably, I'd like to be able to do it in code and without having to actually execute the query just to see it.
Thanks!
there are several approaches to looking at the sql.
Free
On the ObjectQuery do .ToTraceString() that will show u the sql generated for the query.
Download ef tracing provider written by one of the EF team members. EF Tracing Provider
Linq To Entities visualizer which you can download here.
LinqPad
Sqlserver profiler
Commercial
Efprof.com
If you can't get any of the other solutions to work, you could try using the SQL Server Profiler if you have access to the SQL Machine.
Within SQL Server Management Studio you can do the following:
Tools -> SQL Server Profiler.
Create a new Trace and run your code and you should see the queries come across. You can create some filters so you don't see the security / audit stuff which you probably don't care about.
Hi there is this visualizer...but I could not get it to work for me...you could try it..I would recommend LINQPad for viewing your queries you can setup your ef connection and execute your queries.

Is LINQ-to-SQL + SQLite.NET + Stored Procedures + VISUAL STUDIO 2008 possible?

So I want to retrieve data using stored procedures. Preferably via Linq2SQL.
So I can do something like this (example):
var productHistory = dbname.StoredProcedureMethod(value);
foreach(var product in productHistory)
{
//stuff
}
This is something I'd like to be able to do in Visual Studio 2008 in .Net 3.5.
It is possible with Visual studio 2008 + .Net 3.5 + SQL Server 2008.
But firstly, I don't like SQL Server 2008 and secondly, I need the database to be absolutely portable.
I've always liked SQLite.net, but it is not a direct requirement - as long as the database solution is portable.
I haven't been able to set anything up but Visual studio 2008 + SQLite.Net.
So yeah, I am asking for your help :)
Linq to SQL works only with SQL Server, not with other DBMS. As an alternative, you could :
use SQL Server Compact Edition : it is portable, and I think it's compatible with Linq to SQL (could anyone confirm ?)
use Entity Framework rather than Linq to SQL. A few months ago, Microsoft announced they would stop support for Linq to SQL, EF is now the "official" Microsoft ORM. It is extensible and can support any DBMS if an adequate provider is available. And the SQLite.NET provider supports Entity Framework, I've been using it successfully for some time now.
Regarding stored procedures, if you really need them you can forget SQLite, because it doesn't support them.
You are likely to run into a non-movable roadblock: SQLite does not support stored procedures.

Categories