Debugging Entity Framework SQL statements - performance

I am having a weird pattern of response time when using the Entity Framework for SQL communication.
This is from my web host:
This is from my local server:
It's the increase in response time I am worried about.
I have narrowed the problem down to one single line in code
Nop.Data > EfRepository.cs > public void Insert(T entity) > _entities.Add(entity);
Yes I know this very specific for the NopCommerce, but the point is really that I am looking her for help on how to debug this.
Are there some events I can catch that display the SQL being executed?
Or what other things can I do to find out more what is actually happening in the Entity Framework in that above command.

For debugging EF queries, the easiest thing is to cast the query to ObjectQuery and use ToTraceString:
var query = myContext.MyTable
.Where(r => r.Id == searchId)
.Select(r => r);
Console.WriteLine(((ObjectQuery)query).ToTraceString());
This will show the underlying SQL for the query, and you can run the queries manually to debug why they are slow. Here is the MSDN link:
http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx
If you're trying to get the SQL which is run when you call SaveChanges() on your context, it's not as easy. You could take a look at EFTracingProvider:
http://blogs.msdn.com/b/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspx
Or, assuming you use SQL Server, you can go directly to SQL Profiler and capture the T-SQL statements (this is my preferred approach).

In EF6, you can also do this in the constructor of your dbcontext
Example
public BookServiceContext() : base("name=BookServiceContext")
{
// New code:
this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
}
This will log to the console every SQL query EF generates. See this article for more information http://blog.oneunicorn.com/2013/05/08/ef6-sql-logging-part-1-simple-logging/

Another example might be very helpful for someone.
public class MyDbContext : DbContext
{
private readonly ILoggerFactory _loggerFactory;
public MyDbContext(DbContextOptions<MyDbContext> options, ILoggerFactory loggerFactory) : base(options)
{
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Enable logging
optionsBuilder.UseLoggerFactory(_loggerFactory);
if (System.Diagnostics.Debugger.IsAttached)
optionsBuilder.EnableSensitiveDataLogging();
base.OnConfiguring(optionsBuilder);
}
}

Related

using signalR .net core client

I have set up a signalR website .net core. My function in my hub is:
public async Task Notify(int id) {
await Clients.All.InvokeAsync("Notified", id);
}
I have also tested this with the following js:
let connection = new signalR.HubConnection(myURL);
connection.on('Notified', data => {
console.log(4, data);
});
connection.start();
The js code seems to work fine and I see the log when I try connection.Invoke('Notify').
Now I have a console app that can needs to make the invoke. I am trying this in two ways and don't mind either solution:
1. A mvc controller within the signalR website that can take the id and invoke 'Notified'.
2. Use the client library Microsoft.AspNetCore.SignalR.Client in the console app.
The way 1 I have only done in classic asp.net like this:
GlobalHost.ConnectionManager.GetHubContext(hubName)
But couldn't find a way to do this in .net core.
Way 2 I have used the library and tried this so far:
var con = new HubConnectionBuilder();
con.WithUrl(myURL);
var connection = con.Build();
connection.InvokeAsync("Notify",args[0]).Wait();
This is the closest I have come to create a connection in the same way as the js code. However this code throws a null pointer when calling connection.InvokeAsync. The connection object is not null. It seems to be an internal object that is null. According to the stack trace the exception is thrown when a MoveNext() function is internally called.
Well looks like both are not currently possible. As of now I just used a forced way which is hopefully temporary.
I have created and used the following base class for hubs:
public abstract class MyHub : Hub
{
private static Dictionary<string, IHubClients> _clients = new Dictionary<string, IHubClients>();
public override Task OnConnectedAsync()
{
var c = base.OnConnectedAsync();
_clients.Remove(Name);
_clients.Add(Name, Clients);
return c;
}
public static IHubClients GetClients(string Name) {
return _clients.GetValueOrDefault(Name);
}
}
GlobalHost is gone. You need to inject IHubContext<THub> like in this sample.
This can be a bug in SignalR alpha1. Can you file an issue on https://github.com/aspnet/signalr and include a simplified repro?

Entity Framework Compiled Query

How do I write this Entity Framework LINQ Query as a Compiled Query?
var context = new SlxDbContext();
var userSet = context.Set<User>();
User user = userSet.Where(x => x.UserName == "gstrader").First();
There's no way to use CompiledQuery when you're using the DbContext API; CompiledQuery works only with ObjectContext. If you're using Code First, you're most likely using the DbContext API. And Microsoft recommends that you use the DbContext API in new projects even if you'll be working with Database First or Model First models.
But if you use EF5, it brings auto-compiled queries, which work very differently than CompiledQuery. Instead of your writing code to compile each query and then invoking each as needed, EF5 caches the generated SQL for you as a background process, then searches the cache for already compiled queries when you execute any query.
See:
http://blogs.msdn.com/b/adonet/archive/2012/02/14/sneak-preview-entity-framework-5-0-performance-improvements.aspx
and
http://www.devproconnections.com/article/entity-framework/entity-framework-5-143875
Unfortunately the version of EF you are using (code first), does not support compiled queries.
Please correct me if I'm wrong.
Some links:
How do I precompile an Entity Framework Code-First Query?
EF Code First DbContext and Compiled Queries
http://blogs.msdn.com/b/adonet/archive/2011/03/02/ef-4-1-is-coming-dbcontext-api-amp-code-first-rtw.aspx
UPDATE:
Here is a sample for compiled queries, but I think it's not going to work with Code First:
public static Shop CompiledGetShopById(Guid shopId)
{
using (DataContext dtx = new DataContext(ConfigProvider.ConnectionString)) {
return Compiled_GetById.Invoke(dtx, shopId);
}
}
private static Func<DataContext, Guid, Shop> Compiled_GetById =
Objects.CompiledQuery.Compile<DataContext, Guid, Shop>(
(DataContext db, Guid shopId) =>
(from item in db.Shops where item.ShopId == shopId)
.FirstOrDefault()
);

LightSwitch custom query with LINQ using delegates does not work

I have problems in designing an extended query in the ...PreprocessQuery() Method of a LightSwitch query.
1st of all, I tell you in a simplified way, what works:
partial void NeedHelp_PreprocessQuery(bool Admin, ref IQueryable<Device> query)
{
// Some code...
query = query.Where<Device>(d => d.IsAvailable());
}
The query does not show any compile error and does what it should do.
Now, when I try to put the logic into a method and use its delegate in the query, there's no compile error either, but I get an exception. Here is the code:
private bool Logic(Device TheDevice, bool Admin)
{
return Admin ? true : TheDevice.IsAvailable();
}
partial void NeedHelp_PreprocessQuery(bool Admin, ref IQueryable<Device> query)
{
// Some code...
Func<Device, bool, bool> LogicDelegate = new Func<Device, bool, bool>(this.Logic);
query = query.Where<Device>(d => LogicDelegate(d, Admin));
}
The Exception is in German, I try to translate the essential:
The expression is not supported. Expression:
Invoke(value(LightSwitchApplication.ApplicationDataService+<>c__DisplayClass4).LogicDelegate, d, value(LightSwitchApplication.ApplicationDataService+<>c__DisplayClass4).Admin)
Message of inner exception:
The type "ApplicationData.Implementation.Device" cannot be used for a parameter of type "LightSwitchApplication.Device".
Since I am only using Device, I do not understand this confusion between ApplicationData.Implementation.Device and LightSwitchApplication.Device! What am I doing wrong?
Or is this kind of call simply not possible in LightSwitch?
There is no support for inline code in linq2sql - its just imagination ;).
When you do something as 'item.Trim()' behind the scenes this will be translate to the SQL command LTRIM(RTRIM(item))
You may check Views in MSSQL.

Should I trust LinqDataSource to clean up properly?

Taking my first stab as using the OnSelecting method of LinqDataSource so that I can specify a more complex query, I wrote this:
protected void CategoriesDataSource_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
{
using (DataLayerDataContext db = new DataLayerDataContext())
{
e.Result = (from feed in db.Feeds
where feed.FeedName.StartsWith("Google")
select feed.MainCategory).Distinct();
}
}
The problem, of course, is that the using clause will Dispose the DataLayerDataContext. The 'solution' is to write it without it, but I'm afraid then that the context won't be disposed of in a timely fashion, that it will leave a bunch of connections open until garbage collection runs, and so on.
I'm no expert in this area, so any comments on whether this is a real problem, or am I worried for naught?
Ah, another person burned by the unfortunate side effects of deferred loading...
protected void CategoriesDataSource_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
{
using (DataLayerDataContext db = new DataLayerDataContext())
{
e.Result = (from feed in db.Feeds
where feed.FeedName.StartsWith("Google")
select feed.MainCategory).Distinct().ToList();
// ^^^^^^^^^
}
}
This will force the DataLayerDataContext to immediately run the query and create an in-memory list that's not dependent on the context or connection. That way you get your results immediately and you can dispose the context whenever you like.
The only problem (per Steven's comment) is the lazy-loaded navigation properties; even if you force the query to evaluate, if you try to query any properties that are references to other LINQ objects (or lists of LINQ objects) they will fail to load unless you specify what properties are immediately required in the DataLoadOptions of the DataContext. See the below for an example:
protected void CategoriesDataSource_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
{
using (DataLayerDataContext db = new DataLayerDataContext())
{
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Category>(c => c.SomeReference);
loadOptions.LoadWith<Category>(c => c.SomeOtherReferences);
db.LoadOptions = loadOptions;
e.Result = (from feed in db.Feeds
where feed.FeedName.StartsWith("Google")
select feed.MainCategory).Distinct().ToList();
}
}
Once you manually specify these associations, LINQ to SQL will eagerly load them into memory when the query executes, instead of leaving them to deferred loading (which will cause an exception when the properties are used after the DataContext is disposed.)

Fluent NHibernate - how to configure for oracle?

Almost certainly a stupid question but I can't find the answer anywhere.
In the Getting Started tutorial the database is SQLite and so his session factory creation
is done using the SQLiteConfiguration class in the FluentNHibernate.Cfg.Db namespace
Great! But I don't see a Configuration class for using an Oracle database. How do I do this?
Cross-posted to the fluent NH mailing list (with answer)
This works for me. Hope this helps!
private static ISessionFactory CreateSessionFactory()
{
var cfg = OracleClientConfiguration.Oracle9
.ConnectionString(c =>
c.Is("DATA SOURCE=<<NAME>>;PERSIST SECURITY INFO=True;USER ID=<<USER_NAME>>;Password=<<PASSWORD>>"));
return Fluently.Configure()
.Database(cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<CLASS_NAME>().ExportTo(#".\"))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
// this NHibernate tool takes a configuration (with mapping info in)
// and exports a database schema from it
new SchemaExport(config)
.Create(false, true);
}
Does this helps you?
http://tiredblogger.wordpress.com/2008/12/04/persistanceconfiguration-for-oraclefluent-nhibernate/
Edit: The code mentioned uses the ConnectionStringExpression class which no longer exists in Fluent NHibernate. However, that class isn't used for anything other than holding the OracleConfiguration _config field. You can safely, add the field to the OracleConnectionStringExpression class and remove it.
The remaining issue is that NHibernate will now for some reason look for components that are not in the current build of Oracle.DataAccess. If you want to deal with that you can do what tiredblogger did here.

Resources