Wednesday, 16 September 2009

C# 4.0 Peek

What features of C# 4.0 should you care about?  The main push is the dynamic keyword (see this channel 9 video for more).

Dynamic lookup

Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the C# static type checking and instead gets resolved at runtime.

Named and optional parameters

Parameters in C# can now be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted. Furthermore, any argument can be passed by parameter name instead of position.

COM specific interop features

Dynamic lookup as well as named and optional parameters both help making programming against COM less painful than today. On top of that, however, we are adding a number of other small features that further improve the interop experience.


It used to be that an IEnumerable wasn’t an IEnumerable. Now it is – C# embraces type safe “co-and contravariance” and common BCL types are updated to take advantage of that.

Sunday, 23 August 2009

Navigation Properties and Entity Inheritence

Navigation Properties are not supported on derived entity types
The full error message reads something like this:
Navigation Properties are not supported on derived entity types. Entity Set [BaseSetName] has a instance of type [DerivedType], which is an derived entity type and has navigation properties. Please remove all the navigation properties from type [DerivedType].

Sounds straight forward enough, right? Remove the navigation properties from the derived type. Hang on, my navigation properties are specific to the derived type, not the base type. Does this mean I can't navigate from the derrived type at all? As it turns out, that's right - you can't (see Microsoft's response). So, the equation as I see it as follows:
  • You can use inheritance in your EF model but not with data services. You need to write the WCF service and client proxy by hand.
  • Or, you can remodel you entities without inheritance and use ADO.NET data services.
Apparently this is going to be fixed in v2. I am disappointed by this: for me, a big factor in using EF is the support for entity inheritance. Not happy!

Debugging ADO.NET Data Services


ADO.NET data service request fails with the following incredibly unhelpful message: "An error occurred while processing this request":


The cause could be anything, but to find out what the underlying error is you need to enable verbose errors and include exception details in faults: see this post for details.

Entity Framework Metadata Resources

When you create an EF model, the metadata is defined in an .EDMX file. This file contains three sections:
  1. SSDL = storage model: defines the 'shape' of the database in which the data is stored. This includes the tables, their columns and the relationships between tables. All data types are defined in database types e.g., varchar.
  2. CSDL = conceptual model: defines the 'shape' of your entity model. This includes the classes, their properties, and the relationship between classes. All data types are defined in .Net types e.g., System.String or System.Int32.
  3. MSL = mapping model: describes how the conceptual model is mapped onto the storage model. E.g., each property in the conceptual model is mapped to a column in the storage model.
Where does all this information go?

When you compile a project that contains an .EDMX file, three resources are embedded into your assembly. Not surprisingly, these resources are yourContainer.SSDL, yourContainer.CSDL, and yourContainer.MSL (where yourContainer is the name of your entity container). If you open your assembly in .Net Reflector you'll see they contain, verbatim, the XML from each section of the .EDMX.

EntityContainer name could not be found in the mapping and metadata information


ADO.NET entity framework throws the following exception when creating an entity context:

System.ArgumentException: The specified default EntityContainer name [NAME] could not be found in the mapping and metadata information. Parameter name: defaultContainerName
at System.Data.Metadata.Edm.ClrPerspective.SetDefaultContainer(String defaultContainerName)
at System.Data.Objects.ObjectContext.set_DefaultContainerName(String value)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)


This error can mean a mismatch between the metadata referenced in the connection string (i.e., in config) and theembedded resource in your assembly. You have probably noticed that entity framework connection strings contain a much more than typical db connection strings. In fact the db connection is just one of its properties. So what are the other properties?

  1. metadata: lists the three metadata resources required by entity framework (CSDL, SSDL, MSL). For more info on these see entity framework metadata resources.
  2. provider: the .Net data provider
  3. provider connection string: the underlying db connection string (this is the 'typical' connection string). Note that this string is quoted inside the EF CS using "
Now if the names of any of the resources in the metadata property don't match the resources actually embedded in your assembly, then you'll get this error. How do I tell what the names of the embedded resources are? Open your assembly using .Net Reflector. Look under resources - you should see the three EF metadata resources. Check your connection string and make sure they match.

Saturday, 25 July 2009

Getting started with Silverlight

Excellent tutorial of the ABC's of silverlight ...

Silverlight WaterMarkedTextBox gone awol?

VS Intelisense for referenced assemblies

For IntelliSense to be available for referenced binary assemblies, the XML documentation files for each of these assemblies must be placed in the same directory as the referenced assemblies.

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance

User instance mode of SQL Express allows you to bundle a SQL Server database file with your (typically web) application and have it attached to the SQL Express service at runtime. This is useful for development as the entire database is a single file that can be packaged with the rest of the application source.

However, this didn't work for me with the following error:

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance

Wow, that's not too helpful guys. The real problem: I had configured my SQL Express instance to logon as Network Service. User instance mode requires LocalSystem. This can be changed by in SQL Configuration Manager. (Double click on the offending instance and change the logon type to LocalSystem).

ADO.NET Entity Inheritance and Dynamic Data

The short story: it doesn't work! Maybe more detail later ...

Silverlight Project Type not Supported in VS 2008 SP1 after reinstalling tools

This comproblem had me stumped for a while. The solution (simple as ever): open a command prompt to your c:\program files\microsoft visual studio 9.0\Common7\IDE and simply run:

devenv /setup

It may tell you that the operation could not be completed properly but it does mysteriously fix the problem - if anyone knows why I'd love to hear about it!