DataContext.Log equivalent in Entity Framework? - entity-framework

In Link-2-SQL, I can use the DataContext.Log property to see the exact queries that are getting thrown to SQL Server.
Is there an equivalent to this in Entity Framework?

ObjectQuery.ToTraceString()

Since Entity Framework supports multiple backends (as opposed to Linq-to-SQL which is SQL Server only), you can't really get the actual SQL being sent to the backend server from EF.
In order to really see what's going on, I'd recommend firing up SQL Profiler on the SQL Server backend, and see what queries get sent its way.
See this article on Simple-Talk and possibly this video series on becoming a SQL Profiler master if you're not familiar with the SQL Profiler tool.
Marc

Related

PostgreSql or SQL Server 2008 R2 should be use with .Net application using entity framework?

I have a database in PostgreSQL with millions of records and I have to develop a website that will use this database using Entity Framework (using dotnetConnect for PostgreSQL driver in case of PostgreSQL database).
Since SQL Server and .Net are both native to the Windows platform, should I migrate the database from PostgreSQL to SQL Server 2008 R2 for performance reasons?
I have read some blogs comparing the two RDBMS' but I am still confused about which system I should use.
There is no clear answer here, as its subjective, however this is what I would consider:
The overhead of learning a new DBMS and its tools.
The SQL dialects each RDBMS uses and if you are using that dialect currently.
The cost (monetary and time) required to migrate from PostgreSQL to another RDBMS
Do you or your client have an ongoing budget for the new RDBMS? If not, don't make the mistake of developing an application to use a RDBMS that will never see the light of day.
Personally if your current database is working well I wouldn't change. Why fix what isn't broke?
You need to find out if there is actually a problem, and if moving to SQL Server will fix it before doing any application changes.
Start by ignoring the fact you've got .net and using entity framework. Look at the queries that your web application is going to make, and try them directly against the database. See if its returning the information quick enough.
Only if, after you've tuned indexes etc. you can't make the answers come back in a time you're happy with should you decide the database is a problem. At that point it makes sense to try the same tests against a SQL Server database, but don't just assume SQL Server is going to be faster. You might find out that neither can do what you need, and you need to use faster disks or more memory etc.
The mechanism you're using to talk to a database (DotConnect or Microsoft drivers) will likely be a very minor performance consideration, considering the amount of information flowing (SQL statements in one direction and result sets in the other) is going to be almost identical for both technologies.

How to enter SQL log in ADO.NET Entity Framework

I'm using ADO.NET Entity Framework for our business application in ASP.NET website. We're using WCF and LINQ to query the data source. My problem is that data loading from database (for e.g. to load data in gridview) is taking much more time than expected, so we want to log statements in ado.net generated sql statement so we can see which query is taking more time .
How to do this?
I would strongly suggest that you use SQL Profiler rather than creating your own logging mechanisms.
Microsoft SQL Server Profiler is a graphical user interface to SQL
Trace for monitoring an instance of the Database Engine or Analysis
Services. You can capture and save data about each event to a file or
table to analyze later. For example, you can monitor a production
environment to see which stored procedures are affecting performance
by executing too slowly.
In your C# application, in your ConnectionString, add Application Name=yourApp. This will make it easier to locate in SQL Profiler.

WCF Data Service / Oracle - Performance issues

I'm using the following to surface data from Oracle:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/EntityFrameworkWCF/WCFEntityFramework.htm
I have 2 WCF data services one is pointing to a SQL Server database, the other is pointing to an Oracle instance.
The one that points to SQL is lightening fast, the one to Oracle is horrifically slow, upwards of 15-20 seconds returning results.
I tried running the exact same query on Oracle via SQL plus, it works just fine, returns results great. I switched out the data provider from ODP.NET to the Microsoft's .NET provider for Oracle, still no improvement.
http://msdn.microsoft.com/en-us/library/a6cd7c08.aspx
Any thoughts on how I can troubleshoot this?
The issue had to do with 2 things -
1. The ODP.NET driver is painfully slow.
2. Malformed (my bad) queries, when using the restful API, or actually, when using WCF Data services, be sure to be very specific with your queries.

How can I see what TSQL Entity Framework is generating for a query?

Imagine this linq query:
return db.Projects.Where(p => p.ProjectID == id);
How can I see what TSQL it's generating?
I use LINQPad
You can see your queries as lambda expressions or SQL
SQL Server profiler does exactly this. If you are on SQL Server Express, there is an open source alternative called AnjLab Sql Profiler since SQL server profiler isn't included with express.
You can attach the Sql Server Profiler to your database while running the query. Furthermore, have a look at the tracing and caching utility for EF.

entity framework performance

I am using Entity Framework to layer on my SQL Server 2008 database. The EF is present in my web service and the webservice is invoked by a Silverlight client.
I am seeing a serious performance issue in terms of the duration taken by a query to execute in the EF. This wouldn't happen in the consecutive calls.
A little bit of googling revealed that, it's caused per app domain to construct the in-memory model of the db objects. I found this Microsoft link explaining pre-generation of views for performance improvement. Even after implementing the steps, the performance actually degraded instead of improving. I am curious, if anyone has tried this approach successfully and if there are any other avenues for improving performance.
I am using .NET 3.5.
A couple areas to look at for EF performance
Do as much of the processing before calling things like tolist(). ToList will bring everything in the set into memory. By default, EF will keep building the expression tree and only actually process it when you need the data in memory. That first query will be against the database, but afterwards the processing will be in memory. When working with large data, you definitely want as much of the heavy lifting done by the database as possible.
EF 1 only has the option to pull the entire row back. Therefore if you have a column that is a large string or binary blob, it is going to be pulled down and into memory whether you need it or not. You can create a projection that doesn't include this column, but then you don't get the benefits of having it be an entity.
You can look at the sql generated by EF using the suggestion in this post
How do I view the SQL generated by the Entity Framework?
The same laws of physics apply for EF queries as they do for ordinary SQL. Check your database tables and make sure that you have indexes on primary and foreign keys, that your database is properly normalized, and so forth. If performance is degrading after Microsoft's suggestions, then that's my guess as to the problem area.
Are you hosting the webservice in IIS? Is it running on the same site as the Silverlight App? What about the database itself? Is it running on a dedicated machine? Are there other apps hitting it? The first call to a dormant database is painful (I've had situations where it would actually time out in my environment.)
There are a number of factors to take into consideration here. But it comes down to more than just EF's overhead.
edit I didn't fully qualify but the process of opening the first connection to SQL Server is slow regardless of your data access solution.
Use SQL Profiler to check how many queries executed to retrieve your data.If it's large number use Include() method of ObjectQuery to retrieve child objects with parent in one query.