One of the most common questions I get when people see a video or presentation about how I build MVC applications is, “Why don’t you use the repository pattern?” There are a lot of reasons not to use the repository pattern, and they’ve been well-covered by others. But, one valid benefit they provide is encapsulation of query logic. In this post, I’ll show you how you can maintain encapsulation in your applications without having to use the repository pattern. More...
I know, I know, you’re already rolling your eyes! “Stored procedures??!?” But hey, sometimes a stored procedure is the best solution to a problem! Entity Framework allows you to call stored procedures quite easily out of the box, but working with sprocs that return multiple result sets isn’t so easy. Here’s a handy extension method you can use to make it easier. More...
26. January 2015 13:54
Entity Framework supports Enum properties on your model, but there’s one little gotcha that’s bitten me on more than one occasion now. In your database, you may be tempted to save space by configuring the backing column to use an integer value that takes up less space than a normal int, such as a TINYINT (byte) or SMALLINT (short). If you do though, you may get an error like the following: System.InvalidOperationException : The 'State' property on 'Issue' could not be set to a 'System.Byte' value. You must set this property to a non-null value of type 'IssueState'. It sounds like EF is trying to assign the raw Byte value to the property rather than converting it to an Enum member, and indeed that’s what it’s doing. Fortunately, the fix is easy: just change the base type of your enum! By default, enums derive from System.Int32, but you can change that: public enum IssueState : byte
New = 0,
InProgress = 1,
ReadyForQA = 2,
Deferred = 10,
Closed = 20
Now EF will hydrate your entity with no complaints. Just remember to set the base type to match the column type you used in your table!
It’s Friday, which means another round-up of useful links. Oh, and a cat picture! More...
Happy Friday! Here are some random, useful links I have come across over the last couple of weeks. More...
I ran into a frustrating little problem today. I'm getting started with Entity Framework 6 and its async features on a new project, and my SpecsFor integration tests were bombing out unexpectedly. In turns out the problem was caused by TransactionScope and async work together out of the box. Or rather, how they don't work together. More...
2. February 2013 08:56
I’ve been doing some work with Entity Framework 5 lately. Here’s a simple generic repository I created that allows you to “Include” related entities by applying an attribute. More...