Code through the pain Ladislav Mrnka's professional blog about software development


Entity framework contains mapping functionality for entities, complex types and mapping integer columns to enumerations (only when .NET Framework 4.5 Beta and VS 11 is used). Is it enough? I don't think so and because of that I started a new suggestion on Data UserVoice: Support for simpe type mapping or mapped type conversions.

Sometimes we need to map (convert) values for simple types as well. This need for simple type mapping will become much more significant once we try to use Entity Framework with legacy database (or simply database which is not created primarily for Entity Framework). Perhaps these examples can sound familiar:

  • Char column containing values y and n or VarChar column containing values yes and no or true and false - we want to map it to a boolean property
  • VarChar column containing date - we want to map it to a DateTime or DateTimeOffset property
  • VarChar column containing numeric value - we want to map it to a numeric type property
  • VarChar column containing enum value - we want to map it to an enum property

But there can be more advanced cases:

  • VarChar column containing joined values - we want to map to a list of strings
  • VarChar column containing XML - we want to map to XElement or XmlDocument
  • VarChar column containing some identification - we want to map it to a type defined by this identification (for example create CultureInfo from a string like en-us)
  • Binary data which we want to map to a stream or a specific type
  • As the most advanced case we can even think about using multiple columns to get a single property of some specific type (I don't mean mapped complex type in this case)

At the moment this can be achieved only with a workaround in mapped entities where we map the column to the property with the type demanded by Entity Framework and in the same time we expose the second non mapped property doing our conversion internally from the mapped one. That works for .NET code but it has several issues:

  • It is mapping logic = it doesn't belong to the entity. The entity should not need to know anything about the way how it is persisted.
  • It doesn't work if we want to use our new non mapped property for example for filtering in Linq-To-Entities queries. Linq-to-entities queries must use the original mapped property but it means that our mapping logic will even leave the entity and creep into other parts of our code.
  • If Linq-to-entities need to use the mapped property, the property must also be accessible to the code defining the query. It usually means ugly public interface of our entity.

If you feel that this would be a valuable feature in Entity Framework, don't forget to vote for the suggestion on UserVoice because upcoming features in Entity Framework are currently selected only from highly voted suggestions.

Posted on March 4, 2012 by Ladislav Mrnka
Filed under: Entity framework
No Comments

Julie Lerman recently wrote a very nice article about using Entity Framework 4.3 code first migrations with an existing database. Using an existing database is very common scenario so this task will be used often. The mentioned article contains one cumbersome operation where we must create the initial migration and manually clean all code prepared for us because we are using the existing database where these changes must not be executed (it would result in an exception and stop our migration process). ADO.NET team promptly addressed this issue and Entity Framework 4.3.1 released yesterday contains a new command parameter which will create the empty initial migration for us.

The rest of the article will show the whole walkthrough of adding the initial migration and it will especially targets the situation where we are upgrading existing database created by the code first approach with Entity Framework 4.1 or Entity Framework 4.2.

Posted on March 1, 2012 by Ladislav Mrnka
Filed under: Entity framework
Continue reading