You can capture SQL trace messages from an ECO persistence mapper and force an ExpressionHandle object list to load its rows; this page is for developers diagnosing persistence-server queries and loading behavior.
Capture SQL statements from a persistence mapper
An ECO persistence mapper translates operations on modeled objects to persistence storage operations. When the persistence storage is a relational database, SQL trace logging lets you inspect the SQL messages produced by the mapper. See Documentation:Persistence and Documentation:Persistence mappers for the persistence concepts and available mapper options.
The following example adds SQL messages to an in-memory List<string> in a partial persistence-mapper-provider class.
public partial class ModelPMP : Eco.Persistence.PersistenceMapperProvider
{
List<string> _log = new List<string>();
public ModelPMP() : base()
{
EcoListener.StartListening();
EcoLogSwitches.LogSql = true;
EcoListener.Singleton.OnTraceLog += new TraceLogHandler(OnTraceLog);
this.InitializeComponent();
}
private void OnTraceLog(object sender, TraceEventArgs e)
{
if (e.Category.Equals(EcoTraceCategories.Sql))
{
_log.Add(e.Message);
}
}
}
What the example does
| Code | Purpose |
|---|---|
EcoListener.StartListening();
|
Starts the ECO listener so that trace messages can be received. |
EcoLogSwitches.LogSql = true;
|
Enables SQL logging. |
EcoListener.Singleton.OnTraceLog += ...
|
Subscribes the OnTraceLog method to trace-log events.
|
e.Category.Equals(EcoTraceCategories.Sql)
|
Filters the received trace events so that the list receives SQL-category messages only. |
_log.Add(e.Message);
|
Keeps each SQL trace message in the mapper instance's _log list.
|
For example, after an operation causes the mapper to query the database, inspect the strings collected in _log to see the SQL trace messages emitted for that operation. This example collects messages in memory; it does not write them to a file or expose them through a user interface.
Add the logging to your mapper
- Open the handwritten partial class for the persistence mapper provider, such as
ModelPMPin the example. - Add a list to hold the messages.
- In the constructor, start the listener, enable
LogSql, and subscribe toOnTraceLog. - In the event handler, keep the
EcoTraceCategories.Sqlfilter if you want SQL messages only. - Run the operation you are investigating and inspect the collected
_logentries.
Keep handwritten code in the partial class rather than generated code. MDriven separates handwritten and generated code through incomplete (partial) classes; see Documentation:MDriven Architecture.
Load an ExpressionHandle object list
An ExpressionHandle can expose its element as an IObjectList. Call EnsureObjects() on that object list when you want the list's rows loaded in as few batches as possible.
IObjectList il = (ehPerson.Element as IObjectList);
il.EnsureObjects();
In this example, ehPerson is the ExpressionHandle. The code obtains its element as an IObjectList and calls EnsureObjects() before code that needs the rows.
Use this deliberately when investigating or controlling when the result is loaded. Query expressions can be evaluated against persistence storage rather than by first fetching all objects into memory. For background on persistence-storage queries and in-memory queries, see Documentation:A few words on linq.
Choose the right diagnostic log
SQL trace logging in the mapper helps you investigate persistence operations. It is different from transport and server diagnostics.
| If you need to investigate | Use |
|---|---|
| SQL messages produced by the persistence mapper | The EcoListener and EcoLogSwitches.LogSql pattern on this page.
|
| Communication between client and persistence server over WCF | Documentation:WCF issues and Documentation:Using different WCF Bindings with Enterprise Core Objects – ECO – MDriven framework. |
| Turnkey or MDrivenServer runtime log files | Documentation:Turnkey and MDrivenServer logs. |
