Search

Showing posts tagged Entity Framework.

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.

How to unit test a repository that uses DbContext with NSubstitute

A fairly standard set up for me is to have a solution in which I have a Data project that contains an EF6 .edmx file, generated from an existing database. I then split the entities into a separate Entities project, and have a Repositories project that references them both.

The problem was, I wanted to write unit tests against the code, but couldn't work out how to do it. This post shows what didn't work, and then what did.

Persuading the Entity Framework to sort a child collection part II

I previously blogged about a seemingly innocent LINQ problem that had me baffled for ages, which was how you sort or filter an entity’s child collection in Linq. For example, if you want to pull a collection of customers, and include all of their orders from this year, but none from earlier, then there doesn’t seem to be a simple way to do this in Linq. You need to do the query in two stages, the first which builds an anonymous type, and the second which links the to parts of it together. See that post for more details.

I also blogged about the problem of Linq not including child entities when doing joins, which requires you to cast the query to an ObjectQuery so you can use the Include() method on it.

The problem comes when you want to combine the two methods, meaning that your query needs to be constructed in two stages to ensure that the sorting or filtering of the child collection is done correctly, but you also need to cast the final result to an ObjectQuery before you send it out over WCF. The problem arises because you need to enumerate the query before doing the Include(), as that is the only way to ensure that the sorting is done, but calling AsEnumerable() gives you an IEnumerable (reasonably enough), which can't be cast to an ObjectQuery! So what's a fellow to do? Good question, and one that had me going for ages.

Passing custom and anonymous types across code boundaries

Linq is great for grabbing entity objects. The code is simple, and you end up with known objects that you can use.

But, when you want to deal with anything slightly off the beaten track, it gets a bit harder. For example, if you have a Linq query that returns an anonymous type, you can easily manipulate it in the same code block.

Here I detail an early exploration into the world of anonymous types