Print Page | Close Window

Using EntitySQL questions

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3087
Printed Date: 16-Apr-2026 at 9:03am


Topic: Using EntitySQL questions
Posted By: katit
Subject: Using EntitySQL questions
Date Posted: 10-Nov-2011 at 10:23am
Consider this code that I use:
 

esqlString = "SELECT VALUE e FROM " + typeof(T).Name + "s AS e";

As you see, I've got generic class of <T> where T : Entity
 
So, when I need to get records for this entity - I use query above. Problem is that I have to add "s" to specify that I'm querying collection of <T>
 
1. I wonder if I can somehow use entityManager to get collection name if I know typeof(T) ? My main concern is that I'm hardcoding and I'm not sure how pluralizing really works. will it add "s" everywhere or there is exception list and I will get bitten by this one day?
 
2. I noticed that when I started using entity SQL - I don't get caching anymore. I wonder if there any techniques where I can achieve dynamic query and also retain caching? To get you an idea why I'm using esql:
 



Replies:
Posted By: kimj
Date Posted: 10-Nov-2011 at 6:14pm
Hi katit,
 
For the first problem, you can use the EntityMetadataStore to retrieve the entity set name for the entity type, which is what Esql is expecting here.  Something like,
  var entitySetName =  entityManager.MetadataStore.GetEntityMetadata(typeof(T)).DefaultEntitySetName;
 
An Esql query is always a "data source only" query, so doesn't take advantage of caching.  The entities are loaded into the EntityManager cache, however, so you can still do cache-only LINQ queries to retrieve and filter already loaded data.
 
If you switch to dynamic SQL, since these queries are LINQ queries you can take advantage of different query strategies, and cache query optimizations.  Dynamic SQL is actually intended for a situation like yours.   We don't have any full code samples, but there's quite a bit of information on the DRC - http://drc.ideablade.com/xwiki/bin/view/Documentation/dynamic-queries - http://drc.ideablade.com/xwiki/bin/view/Documentation/dynamic-queries .
 
 


Posted By: katit
Date Posted: 10-Nov-2011 at 6:35pm
Thanks for response! I already found that info on DRC. Reason I went with ESQL so I can decouple my model from my UI little better.
LINQ sounds much better from compile time validation standpoint. I will look over my code once again and see if I can use it.



Print Page | Close Window