I am currently using the Entity Framework with underlying Oracle connection.
Sometimes, there are Oracle exception returned (foreign key violated, EF-generated SQL not compatible with some old Oracle version, etc.)
Although I can see the high level message, it is difficult to tell what is exactly happening without knowing the error SQL statement passing to the DB. For example, when it is complaining foreign key, I have no idea which ID it is referring to.
Checked the OracleException/DbException, I cannot find any where I can get the Sql statement and the binded parameters of that statetment.
The only solution I can think of is to log the EF generated statement explicitly each time before its execution by my own.
Is there any elegant way to do so?
================================================
Update:
How do I view the SQL generated by the Entity Framework? is the same way (The only solution I can think of is to log the EF generated statement explicitly each time before its execution by my own.) as mentioned in the original question. I do not want to do so because I need to get the generated SQLs each time and log them explicitly.
For example, normally we get the value in the following way:
var shop = (
from shop in shops
where shop.ID = xxx
select shop.Name
).FirstOrDefault();
If I need to get the query string and the bounded parameter(s) like below, it is quite tedious.
IQeuryable shop = (
from shop in shops
where shop.ID = xxx
select shop.Name
)
Log("Sql : " + shop.ToQueryString());
Log("Parameter ID: " + xxx);
It works, but not elegant.
The question is how can I get those info from OracleException (or somewhere else) without logging them explicitly in advance so that when I get the Exception, I can log them directly based on the Exception. Below is an example what I want to do (Those properties do not exist in the Exception object)
try
{
// EF to Oracle query
}
catch (OracleExcetpion ex)
{
Log("SQL: " + ex.SQL.ToQueryString());
foreach (var boundParameter in ex.SQL.BoundParameters)
{
Log("Parameter " + boundParameter.Name + ": " + boundParameter.Value.ToString());
}
}