Folks, this is just a heads-up about a gotcha with Entity Framework 4.

 

Both of my EF4 books warn you that if you want to use the MetadataWorkspace
class to inspect the model, then you cannot guarantee that the metadata has
been loaded and you might throw. Their workaround is to make a call like
context.MyTable.ToTraceString() before you start, optionally using a TryXXX
method around it if you want to be fancy.

 

I haven't seen any warnings of a similar problem when you run an ESQL query.
Yesterday I had a simple query like the following throwing and telling me
that CustomerID is not a member of Customer. I thought I had a missing
namespace or syntax error or whatever, and after terrible suffering I
realised that there was actually nothing wrong with the query and adding the
highlighted line made it work. The error message was completely misleading.

 

using (var context = new TestContext())

{

    Context.Customer.ToTraceString();    <==== FIX

    var query = context.CreateQuery<DbDataRecord>(SELECT c.CustomerID,
c.Name FROM Customer AS c);

    foreach (var rec in query)

    {

        Trace("{0} {1}", rec.GetInt32(0), rec.GetString(1));

    }

}

 

This is a shocking irritation. The equivalent LINQ query works fine, but the
ESQL fails. This means I have to put guard code before every ESQL query I
run (or try to prefer using LINQ when possible).

 

Greg

Reply via email to