EF4 – New Properties for Entity Properties?

If you are working with the Beta 1 of VS2010 in an Entity Data Model, you might notice some new properties in the Properties Window for an entity’s scalar property.

propertyfacetsA

Are they new properties? Actually only one of them is.

Let’s first change the view to see how these properties are grouped.

propertyfacets

Notice that the Fixed Length, Max Length and Unicode are grouped in “Facets”, not in “General”. These facets are actually not new. They exist in the first version of Entity Framework, however they were not exposed in the designer.

But why are they facets and not listed under General with the other properties?

These facets are actually ignored by Entity Framework. For example, if in code you set IngredientName to something longer than 100 characters you will not get an exception. And if you continue on and call SaveChanges, the EF will not notice the discrepancy when it validates constraints prior to building the Insert or Update command.

So what is their point?

They are there for other tools to leverage. For example, the Dynamic Data tooling in Visual Studio reads the facets when it is automatically building web pages from the model. There’s one other purpose, which I’ll bring up shortly.

In the VS2008 SP1 version of EF, nobody knew those facets were there unless they opened up the XML. But now with them being “up front and center” in the designer, I fear that developers will assume that EF will do some validation checking based on them. So be aware that this is still not the case.

And what about StoreGeneratedPattern?

StoreGeneratedPattern is an attribute in the store schema to identify fields such as an identity field whose value is auto-generated by the database. This does not change in EF4. So why are we seeing this in the conceptual model? Notice the group it is placed in. Database Script Generation. This is for the new model first design support in EF. If you are designing a model and then creating a database from that model, you’ll need to inject this logic somewhere. You can define it in the model here so that this attribute will be included when the column is being defined during the database script generation.

In the XML, it is noted with a namespace reference:

<Property Name="IngredientID" Type="Int32" Nullable="false" store:StoreGeneratedPattern="Identity" />

The Fixed Length, Max Length facets are also used by the script generation. For example a string with FixedLength=true will become an nchar while FixedLength=false will result in nvarchar. For SQL Server, wIth nvarchars, Max Length left empty or set to, for example, 100, will mean the difference between an nvarchar(max) and nvarchar(100) in the generated database.

#1 SlimShaggy on 4.24.2010 at 10:09 AM

It is rather upsetting that StoreGeneratedPattern works only in Model First and not vice versa. If one could set this in the conceptual schema and it was reflected in the SSDL generated by "Update model from database", it would greatly help in a situation when an entity has a non-nullable property with a default value set in the database schema. Currently in this scenario I have to manually edit the SSDL to insert 'StoreGeneratedPattern="Identity"' for every such property in my model after every run of "Update model from database" - that's a pain. Again, in Linq to SQL this was much easier!