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


I finally had some time to finish reading of Programming Entity Framework DbContext by Julia Lerman and Rowan Miller. This book is like the second part of Programming Entity Framework Code First I reviewed last time. I liked this book even more than the first one. Again the book covers exactly what is said in the title because it is definitive guide to using Entity Framework DbContext API. Both books together form an ultimate resource about Entity Framework 4.1 (and newer versions) containing some interesting information not mentioned elsewhere. Authors wrote set of two books providing detailed answers to all commonly asked questions on Stack Overflow and MSDN forum.

Again my biggest complain goes to missing index in the electronic edition of the book bought directly on O'Reilly. It is not a big problem in electronic version but it would be a big disappointment if the index is not present in paper version.

There are still few topics missing in the whole Entity Framework puzzle that are not described in either of these books. These topics include mapping with designer in database and model first approaches which are possible with DbContext API as well. These topics remained same as in Entity Framework 4.0 covered in previous great Entity Framework resource: Programming Entity Framework, 2nd edition by Julia Lerman. Combining new books with few selected topics from the last mentioned book will form a knowledge base we need to learn, understand and successfully use Entity Framework and DbContext.

Programming EF DbContext Cover

I found one general suggestion at page 79 where I disagree with the book because it is incomplete. This suggestion describes importance of detaching entities when not using POCOs and claims that detaching is not needed when using POCOs. The suggestion is correct if we use pure POCOs with no lazy loading or change tracking proxies (such classes are used by examples in the book) but in case of proxied POCOs the original importance of detaching entities is still in place.

Posted on May 28, 2012 by Ladislav Mrnka

Eager loading is one of core features in Entity Framework. It allows us loading related entities with main entities in a single round trip to the database. This feature should be extended in the future because the current implementation has several limitations:

We can't specify conditions for eager loading

A lot has been written about this limitation including existing highly voted UserVoice suggestion. The current implementation always loads all related records so if we have a main entity which has thousand related entities and we want to get only 10 most recent through eager loading we don't have a direct way to achieve that. We can use a projection to a custom type or an anonymous type to overcome this limitation but that is just a workaround for the missing feature. Eager loading should offer possibility to limit eager loaded data through conditions and to limit number of returned entities - generally strongly typed Include method should support extension methods like Where, OrderBy and Take.

We can't declare eager loading globally

The current implementation demands eager loading to be specified in every query through Include extension method. In most cases the eager loading definition can be wrapped to a separate extension method and reused so the global eager loading would be just a nice to have feature but there are situation where current Include method doesn't work - for example eager loading on inheritance hierarchies is problematic. Entity framework should offer additional ways to define eager loading. I can imagine two such new ways:

  • Global eager loading defined directly in mapping as proposed in already existing UserVoice suggestion
  • Context scope eager loading defined per context instance - similar mechanism was available in LINQ-to-SQL and its DataLoadOptions

Both these implementations would force the query engine to automatically use eager loading of related entities every time the main entity is queried. They should work on inheritance strategies but also for example when the main entity is queried through lazy loading (you will lazy load entity with all relations configured for global eager loading).

We can't specify how should eager loading be executed

At the moment all eagerly loaded data are joined to one result set. This can be absolutely fine in some cases (eager loading of reference property or small collection for few entities) but it can have a significant impact on larger queries with a lot of eagerly loaded data - I described this problem in the answer for the question asked by puretechy on Stack Overflow. We should be able to specify if eager loading should use a join or a separate query to load related data. All details to build the separate query automatically are already present in the main query where we are using eager loading. As a workaround we can specify and execute those separate queries manually but the disadvantage is that Entity Framework doesn't offer any way to batch multiple queries to single round trip to the database. I posted the related suggestion to UserVoice yesterday.

All these features would greatly improve our development experience when using Entity Framework and eager loading. They will also fit to the most required feature on UserVoice: Improve SQL Generation because possibility to control how eager loading is executed and how many data are really loaded is nothing more than improved control over generated SQL.

Posted on April 16, 2012 by Ladislav Mrnka
Filed under: Entity framework

Last week I finished reading of Programming Entity Framework Code First by Julia Lerman and Rowan Miller. I am quite impressed by the content of the book because it contains exactly what the title says and nothing more. It's the definitive guide to using code mapping in Entity Framework but it doesn't cover any additional topics you are not interested in. It keeps the book thin (the real content has 176 pages) so you can finish it within less than one week and use it as reference for your upcoming experience with Entity Framework Code first. The drawback of the book's size and content is that you should not use this book for learning Entity Framework itself. You should first go through some tutorials, trainings or other books before you use this book because this book will not tell you anything about Entity Framework itself. I recommend this book to anybody who has some basic experience with Entity Framework and who is going to use or who is using Entity Framework Code first in his projects. The biggest problem I see in the book is missing index - at least digital versions bought directly on O'Reilly don't  have any. Using technical paper book without index as a reference can be pretty annoying.

Programming EF Code  First Cover

Side note: The book targets Entity Framework 4.2 so small changes in Entity Framework 4.3 and code first migrations are not covered. There is only very small chapter (2 pages) about migrations explaining what will migrations bring to Code First development approach.

Posted on April 1, 2012 by Ladislav Mrnka

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

Microsoft today released multiple highly anticipated products: Windows 8 Consumer preview, Windows Server 8 Beta, .NET Framework 4.5 Beta, Visual Studio 11 Beta, Visual Studio 11 Team Foundation Server Beta and several related applications, tools or toolkits. If you work with Microsoft technologies and want to check what's new, it is the best time to download this stuff and install some virtual machine for exploration of new features, tools and APIs.

.NET Framework 4.5 also contains a new version of Entity Framework Core libraries (ObjectContext API). This new version contains all features previewed in Entity Framework June 2011 CTP = enums, spatial types, mapped table valued functions and other features are finally here. The new Entity Designer presented in June 2011 CTP is direct part of Visual Studio 11. Together with this release ADO.NET team released Entity Framework 5.0 Beta (DbContext API) which is able to use new features from Entity Framework Core libraries. Some features are still available only when used with database first approach (EDMX) - for example mapped table valued function but features like enums, spatial types, auto-compiled LINQ queries or performance improvements are available for code first as well.

Posted on February 29, 2012 by Ladislav Mrnka
Filed under: Entity framework

Do you remember Entity framework June 2011 CTP with all that nice features? This CTP was initially released as first try for out-of-band (independently on .NET Framework) release of core .NET library - System.Data.Entity.dll. The out-of-band release caused some issues because we had to use separate .NET target for using it and it makes incompatible with other tools and features. Last week ADO.NET team announced that they will not be able to release core functionality out-of-band. It means that all changes and major features / improvements can be released only with a new .NET framework version. The team also announced that we can expect features from June 2011 CTP in upcoming .NET Framework 4.5. What does it mean? .NET Framework 4.5 should contain new version of Core Entity framework libraries (that is the new name for ObjectContext API and Entity framework core features). The rest of this article describes all changes related to upcoming release including new features, missing features and relation to DbContext API.

Posted on October 24, 2011 by Ladislav Mrnka
Filed under: Entity framework
Continue reading

Last week I had to prepare simple security configuration for WCF service - I needed to configure a test service and a client using mutual HTTPS to achieve secure transport and X.509 Binary Token to authenticate requests. Requests should contain only a message data and a security token and no other security headers like a timestamp were allowed. This proved to be more challenging and frustrating than I expected. I started with the simplest possible configuration using CertificateOverTransport authentication mode but it failed with the exception:

System.InvalidOperationException: Signing without primary signature requires timestamp

I found that I'm not the first one who dealt with this problem. MS Connect contains related  "bug" where I expected to find a solution or workaround because WCF is API designed for interoperability with other platforms, isn't it? Surprisingly I didn't find a solution but instead some very odd answer describing that behaviour is "by design". Well, in the context of the used configuration it is really by design as I will describe in this post but the point of the bug was how to do it and Microsoft representative completely failed to answer the question and marked the problem as fixed because of "by design". This really made me furious so I even wrote my own question to MSDN forum. The rest of this article describes the whole route to the working solution as well as description why initial solution didn't work.

Posted on August 31, 2011 by Ladislav Mrnka
Filed under: WCF
Continue reading

Few week ago ADO.NET team published minor update to Entity Framework 4.1 (download page). By following ADO.NET team blog, I guess the purpose of this minor update was fixing few bugs, adding support for context factory and preparing support for Code First Migrations August 2011 CTP  which demands this update. Everything looks awesome because ADO.NET team is fixing some bugs, adding new features and working on new tools (EF power tools, migrations) and new version Entity framework. Still sometimes there can appear some unexpected problem. The mentioned update contains one annoying hidden breaking change reported by many developers using external profilers. The rest of the article will describe this problem, its impact and announced fix.

Posted on August 21, 2011 by Ladislav Mrnka
Filed under: Entity framework
Continue reading

I didn't write almost any article during past three months because I decided to pass several Microsoft certifications and I spend most of my free time be preparing for required exams. Two exams I definitely wanted to pass were:

Well, I passed those exams but I really didn't enjoy the experience. Especially the second exam is probably the worst one I have ever taken. In the rest of the article I will describe some of my opinion about these exams. Be aware that if you are looking for exam questions you will not find them here and I'm not going to share any detailed content of them.

Posted on July 31, 2011 by Ladislav Mrnka
Filed under: Entity framework
Continue reading