try-catch-FAIL

Failure is inevitable

NAVIGATION - SEARCH

Encapsulating Query Logic in MVC Apps Without the Repository Pattern

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...

Returning Multiple Result Sets from a Stored Procedure Using Entity Framework 6

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...

Entity Framework, Enums, and TINYINT

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!

Random Link Roundup–12/4/2014

Happy Friday, everyone!  Today we have a zombie picture, because my house is contaminated with a mixture of flu and stomach virus that I’m pretty sure will lead to the scene below.   Anyway, on to the links! More...

Random Link Roundup–9/25/2014

It’s the last Friday of September.  Time for another cat pic and some links! More...

Random Link Roundup–9/19/2014

It’s Friday, which means another round-up of useful links.  Oh, and a cat picture! More...

Making TransactionScope Work with async/await in .NET 4.5

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...

A Generic Entity Framework 5 Repository With Eager-Loading

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...