SQL from EntityDataSource - entity-framework

Is there an easy way to see SQL statements generated by EntityDataSource?
SQL Server profiling/tracing is not an option here.

I used to use NHProf a profiler for NHibernate and it is awesome. So I can surely say that you should try the Entity Framework Profiler
I just checked and, as for NHProf, there is a free trial version.

You can cast to ObjectQuery and call ToTraceString:
ObjectSet<User> objectSet = ObjectSet;
var query = (ObjectQuery)(objectSet.Where(u => u.LastName == "Doe").Select(u => u));
string trace = query.ToTraceString();
For tracing/caching you can try the EF Caching and Tracing Provider Wrapper. I haven't had a chance to try it yet, but it's definitely on my to do list.

Related

Can't use filter with dynamic linq

I tried to use ef plus for filtering data with following code
_context.Filter<TEntity>(x => x.Where(q => "q.Property == 1"));
And got an error.
Am i doing something wrong?
Disclaimer: I'm the owner of the project Entity Framework Plus
Disclaimer: I'm the owner of the project Eval-Expression.NET
As specified in the introduction: http://entityframework-plus.net/linq-dynamic, LINQ Dynamic require the library Eval-Expression.NET (paid library) to work.
It should now work once you add this library in reference: https://www.nuget.org/packages/Z.Expressions.Eval/

Merging entity with Play! framework and JPA silently failed

I've been looking around and around without finding any topics related to my situation.
I'm using:
Play! framework v2.5.3 in Java
Hibernate EntityManager v5.1.0.Final
Hibernate JPA 2.1 API v1.0.0.Final
PostgreSQL 9.4
Here the route called with AJAX:
PUT /admin/entity/:id
Which is bound to:
controllers.Entity.update(id: Long)
Here how I handle the update request:
#play.db.jpa.Transactional
public Result update(final long id) {
EntityManager em = _jpa.em("default");
DynamicForm form = _formFactory.form().bindFromRequest();
models.Entity entity;
entity = em.find(models.Entity.class, id);
if (entity == null)
return badRequest();
entity.update(em, form);
em.merge(entity);
return ok();
The method update of Entity change values of the class attributes which are basically String attributes.
My issue: nothing get updated while still executing this piece of code.
I enable SQL log which only display the SELECT query corresponding to em.find() method call. Nothing related to an UPDATE query.
I've been using JPA/EntityManager with Play! for others projects (but with lower version of the framework) without facing this kind of problem.
Any idea why nothing get merged ?
I've been able to fix this issue by writing following piece of code inside
models.Entity.update:
em.getTransaction.begin();
Query query = em.createQuery("UPDATE entity SET value = :v WHERE id = :id");
query.setParameter("value", value);
query.setParameter("id", id);
query.executeUpdate();
em.getTransaction.commit();
But even if this is working, that's not the way how thing should be done... It
doesn't make coffee at all!
Edit: this solution do not work anymore....
I really don't know what I'm doing bad, if any body has an idea about this issue, you're help would be much appreciated.

Entity Framework first Access to Database

Everytime when I open the database connection, the very first interaction with the database takes a lot of time. From the second interaction on the "speed" is much higher. I think it's an optimization of the Entity Framework and it looks like something good. But it's still the same even if I'm working with more than one database. The very first interaction with the first database is slow. But the first interaction with the second database is fast. Why isn't the first interaction with another database slow too? Another problem is that the Initializer only works with the first database. (I think this problem is caused by this optimization)
Related to this question (asked by myself):
Entity Framework 6 SetInitializer DbContext does not work for SQL Server Express and SQL Server Compact
Does anyone know how this optimization works and how to disable it? I don't actually work with the databases I compare them, for this case it would be nice to disable any optimization.
I'm wrote an answer as comment is not appropriate.
For the speed Yuliam pointed you to the good link.
For the rest, I'm not sure to understand your problem.
For me the following code (pertinent extract)
class Program {
static void Main(string[] args) {
Database.SetInitializer<TestEFContext>(new DropCreateDatabaseAlways<TestEFContext>());
String cs = #"Data Source=ALIASTVALK;Initial Catalog=TestEF;Integrated Security=True; MultipleActiveResultSets=True";
using (TestEFContext ctx = new TestEFContext(cs)) {
Console.WriteLine(ctx.Orders.Count());
}
cs = #"Data Source=ALIASTVALK;Initial Catalog=TestEF2;Integrated Security=True; MultipleActiveResultSets=True";
using (TestEFContext ctx = new TestEFContext(cs)) {
Console.WriteLine(ctx.Orders.Count());
}
}
}
well create and recreate databases TestEF and TestEF2.
Did I misunderstand your question ?
===== Response to comment:
In this case both databases are always (re)created:
first run: creation
use SSMS to populate some data in some tables
second run
check tables content with SSMS : tables are empty <=> databases were recreated at second run.
Only trivial difference with your case: I use EF 6;

Identify Code Origin from EF-Generated SQL?

What is the best way to identify the source of a (very) inefficient Entity Framework query when I only have the captured SQL available?
Assuming you're using some kind of logging framework, you could do something like this in your DbContext constructor:
this.Database.Log = sql =>
{
if (sql.Contains(/* part of the offending query */))
{
Logger.Log($"Found it! {Environment.StackTrace}")
}
}

Entity Framework 4.0 - The underlying provider failed on Open

We have a web application with Entity Framework 4.0. Unfortunately, when the large volume of users hit the application the EF throws an error
The underlying provider failed on Open
Below is the code snippet:
//DAL
public IQueryable<EmployeeEntity> GetEmployeeDetail()
{
DatabaseEntities ent = new DatabaseEntities(this._connectionString);
IQueryable<EmployeeEntity> result = from employee in ent.EmployeeEntity
select employee;
return result;
}
Please note the above code returns IQuerable.
Is anything wrong with above pattern that could cause the exception to occur?
When and how does Entity Framework determine to close / open db connection and also how long to retain?
On what scenario does above error occurs?
What's the maximum number of connection pool for EF and how do we configure?
Do we need to explicitely specify open and close
Is code below a best way to resolve above issue?
public IQueryable<EmployeeEntity> GetEmployeeDetail()
{
using (DatabaseEntities ent = new DatabaseEntities(this._connectionString))
{
IQueryable<EmployeeEntity> result = from employee in ent.EmployeeEntity
select employee;
return result.ToList().AsQuerable();
}
}
The ToList() call will cause the query to run on the database immediately and as this is not filtered in any way will return every employee in your database. This is probably likely to cause you performance issues.
However you can't remove this in your case because if you return the IQueryable directly then the context will be disposed by the time you try and fetch results.
You could either:
change the way it works so that the scope of ent does not end when the method returns and return the query without calling ToList(). You can then further filter the IQueryable before calling ToList().
call ToList() within the method but filter/limit the query first (e.g. pass some parameters into the method to specify this) to reduce the number of rows coming back from the database.