Daily Archives: September 4, 2007

Nullables in lambda expressions in LINQ to Entities – today’s gotcha

It took me a while to figure out what caused this problem, so I thought I’d share it here.

I was trying to write a query in VB to grab customers who have orders placed after July 1, 2007.

The query looks like this:

Dim q = From cust In nwentities.Customers _
  Where (cust.Orders.Any(Function(o) _
     o.OrderDate > New DateTime(2007, 7, 1)))

But it would not compile. I have Option Strict On and the error was

Option Strict On disallows implicit conversions from ‘Boolean?’ and ‘Boolean’.

What the heck was this “Boolean?”. Not google-able, that’s for sure!

Then I noticed that OrderDate was being defined as a “Date?”.

What I’m seeing is the shortcut for Nullables in VB. It’s really hard to google for that. But if you look it’s everywhere! Such as in this post by Bill McCarthy. (See Bill? I found it all by myself! :-))

OrderDate is nullable. “Date?” means nullable date.

And the VB’s compiler is casting the entire expression to a nullable Boolean then telling me “Yo! A Nullable boolean is NOT the same as a boolean! Sorry”.

Note that the designer showed Nullable as false but the actual database has Nullable=true.

So, while I can write a regular query with this model, such as:

  Dim ords = From ord In nwentities.Orders Where ord.OrderDate > New DateTime(2007, 7, 1)

and I can write my exact query in C# with no worries:

   var q=from cust in nw.Customers where cust.Orders.Any(o=>o.OrderDate>new DateTime(2007,7,1)) select cust;

I believe that in my scenario, I just need to rethink my query. But later…

Ripening the tomatoes

It has been an amazing summer for growing. there are still lots and lots of tomatoes in my garden. I’ve been picking the tomatoes before they are ripe and letting them finish in the sun. I decided that this will help the rest of the tomatoes that are still growing on the vines. I have a whole bunch in the dining room ready to be processed but waiting for this batch so I can do them all at once. There are a few different varieties.

Sharing Entity Framwork models between projects (or teams) when developing

The new Entity Framework tools create an EDMX file which contains all three models: Conceptual, Mapping and Storage, in one file. When your project is built, individual files are created for the models, in the [familiar to those who have been using Entity Framework already] CSDL (conceptual), MSL (mapping) and SSDL (storage) files.

The connection string used by the Entity Framework contains not only the database connection string, but a metadata parameter with pointers to the three mapping files separated by a pipe character.

The Default Locations

Assuming that you put your EDM in the main folder of your project, in Windows Forms, Console and Class Library projects, you will see by default:

connectionString=
metadata=.\Model.csdl|.\Model.ssdl|.\Model.msl;provider=System.Data.SqlClient;provider connection string=’Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorksLT_Data.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True'”

When the project is built, those 3 files will land in the debug or release folder of the BIN directory for the app.

In a web app, the default location is different:

connectionString=
“metadata=~/bin/Model1.csdl|~/bin/Model1.ssdl|~/bin/Model1.msl;provider=System.Data.SqlClient;provider connection string=’Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorksLT_Data.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True'”

This pushes the files into the bin folder of the site as well.

Sharing EDM with another project or another developer

If you have your EDM in it’s own project and then want to reference it from another project, you need to perform two steps.

1) Add a reference to the compiled DLL which represents the EDM.
2) Modify the config file of your consuming application to point to the location of the csdl, msl and ssdl files.

Here is an example of  a web.config’s connection string at design time that needs to use a model which is in a different project folder. I had to manually enter this long file path. Yucch.

connectionString=”metadata=C:\Documents and Settings\julie\My Documents\Visual Studio 2008\Projects\EFBeta2Windows\AdventureWorksModel\bin\Debug\Model.csdl|
 C:\Documents and Settings\julie\My Documents\Visual Studio 2008\Projects\EFBeta2Windows\AdventureWorksModel\bin\Debug\Model.ssdl|
 C:\Documents and Settings\julie\My Documents\Visual Studio 2008\Projects\EFBeta2Windows\AdventureWorksModel\bin\Debug\Model.msl;
provider=System.Data.SqlClient;
provider connection string=’Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorksLT_Data.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True'” providerName=”System.Data.EntityClient” />
   

There’s a better way

In the ADO.NET Orcas forums, Craig Lee pointed out that the build task for EDMDeploy which creates these files is placed in the project file and can be edited. I went looking.

You can get at it right in the UI for editing the project.

Go to the Compile tab, then to Build Events.

You can see the EdmxDeploy code in the Post-build event command line.

By editing, you can see the whole thing and the target directory parameter.

I changed the target to point to an existing (easy access for demos!) folder on my drive.

Then after I built the model project again, the csdl, msl and ssdl files were all in c:\efmodels\aw.

Now I can easily modify the metadata paths of any projects that consume my model to simple paths such as “C:\efmodels\aw\model.csdl”.

If I am in a team environment, then my files need to be available on a shared location or from source control.