Validate Insert statement when using LINQ SubmitChanges

I want to see how my insert statement will look like I was connecting using the ADO.NET text command. How can I do it?

I used the following link:

http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

And they added the DebugTextWriter class to my project. So now, in my code, I have the following that actually does nothing, and I do not consider it correct:

using(WorkbookDataContext dc = _conn.GetContext()) { if(profile.ProfileId > 0) { dc.Profiles.Attach(profile, true); } else { dc.Profiles.InsertOnSubmit(profile); } dc.Log = new DebugTextWriter(); #if DEBUG dc.Log = new System.IO.StreamWriter("linq-to-sql.log") { AutoFlush = true }; #endif dc.SubmitChanges(); } 

Any ideas what I'm doing wrong and / or how to check LINQ insert statement correctly?

thanks

+4
source share
1 answer

A practical guide. Display generated SQL (LINQ to SQL)

You can view the SQL code using the Log property.

Example: use the Log property to display SQL code in the console window before executing the code. You can use this property with queries, inserts, updates, and deletes of commands.

 db.Log = Console.Out; IQueryable<Customer> custQuery = from cust in db.Customers where cust.City == "London" select cust; foreach(Customer custObj in custQuery) { Console.WriteLine(custObj.CustomerID); } 

These lines from the console window are what you see when you execute the above C # code.

 SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactT itle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Coun try], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] = @p0 -- @p0: Input String (Size = 6; Prec = 0; Scale = 0) [London] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20810.0 AROUT BSBEV CONSH EASTC NORTS SEVES 

Alternatively, you can use the LINQ to SQL Debug Visualizer to hover over a LINQ expression while in VS 2008, and then check the raw SQL that ORM will execute at run time when evaluating the LINQ query expression.

+1
source

Source: https://habr.com/ru/post/1311665/


All Articles