Latest posts

How to turbocharge your Entity Framework queries - Part 3

How to turbocharge your Entity Framework queries - Part 3

Note:  This assumes you have read and understood the fab articles  How to turbocharge your Entity Framework queries - Part 1  and  Knowing the difference between IEnumerable<T> and IQueryable<T> . If not, please read them first.

It is important to understand where your code is executed when querying with Entity Framework, as this can make a big difference to the time required to execute your queries.

How to set default values for string properties without having them overridden by our initialising code

How to set default values for string properties without having them overridden by our initialising code

Like many, our database has rather a lot of naughty nullable columns that really should be non-nullable, so we end up with null data issues all over the place. One way to avoid this is to modify the property in the .edmx file to add a default value. However, this is slow and painful, and easy to forget.

As there is no use case we can foresee where you would want a null string property, we have taken the step of adding a constructor to the entity (generated in the T4 template) that initialises every string property to an empty string. This means that whenever you create a new entity, any string properties will be initialised, avoiding any null values.

However, this leaves you with a problem if you want to set a (non-empty) default value for a string.