Calling Stored Procedures from Entity Framework 6 Code First

Just a quick solution to something I was banging my head against for a while.

If you use the EDMX/Database First way of creating your DbContext, you can import Stored Procedures to your context. The result is something like this:

public virtual ObjectResult<Customer_LoadStatus_Result> Customer_LoadStatus(Nullable<int> customerId)
    var customerIdParameter = customerId.HasValue ?
        new ObjectParameter("CustomerId", customerId) :
        new ObjectParameter("CustomerId", typeof(int));
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Customer_LoadStatus_Result>("Customer_LoadStatus", customerIdParameter);

If you try to copy this method to your Code First context, you’re likely to get an error saying something like, “The FunctionImport ‘Customer_LoadStatus’ could not be found in the container ‘CustomerDbContext’.” There’s probably a way to register it in the ModelBuilder, but 15min of web searches came up with nothing (maybe my Google-Fu is weak?).

Long story short, it seems you can’t just copy this to your context if you’re using Code First. Instead, do this:

public virtual Customer_LoadStatus_Result Customer_LoadStatus(int? customerId)
    var customerIdParameter = customerId.HasValue ?
        new SqlParameter("CustomerId", customerId) :
        new SqlParameter("CustomerId", typeof(int));

    return this.Database.SqlQuery<Customer_LoadStatus_Result>("Customer_LoadStatus @customerId",

As a bonus, in my example I’m just returning the object I expect rather than the wrapped one you’d get from the generated code.

.NET, TFS and VS announcements at Connect();

Earlier today, at the Microsoft Connect(); event, there were a bunch of announcements around the .NET platform, Visual Studio 2015, and Visual Studio Online.

There are far too many to list every one, so here are a few of my highlights.

Open Sourcing the core .NET stack

This is huge.

Microsoft has jumped into the Open Source community in a big way recently, particularly with ASP.NET, but now they’ve announced they’re open sourcing the entire .NET stack!

Well, not the entire stack, just the core stuff. This has been met with some scepticism, however I think it’s the right thing to do. Think about the codebase you’re working on right now. If you were told to open source it, would you want to do a bit of work to it first? Maybe refactor a whole lot of stuff?

I’m only guessing, but I would think there’s a bit of work to do before the entire stack is “open-sourceable”. And I believe they’ll do it as quickly as they can.

You can see it all now at

Release Management Service for Visual Studio Online

You may have heard me talk about Release Management at Tech Ed NZ this year. Well, what was previously an on-premises product only is now available in Visual Studio Online as well.

Release Management Service for VSO

Release Management Service for VSO

It’s a relatively limited offering right now, only allowing releases to Azure, but rest assured the new features will start coming thick and fast.

Read more about it on Brian Harry’s blog.

A new Community edition of Visual Studio

No, I’m not talking about Visual Studio Express. This is a much bigger product than that. Instead of thinking “Express with some extra features”, think “Professional with some tiny things taken out”. The biggest difference is the fact that you can install plugins. Yep, in the community edition, you can now run Resharper and Web Essentials.

This is awesome news for startups and open source projects.

Download it now at

ASP.NET 5 and MVC 6

Yep, the .Net web-dev universe is rebooting again. Well, not really, but there are some big changes. The biggest and most important change for me is that MVC and Web API are now in the same pipeline. That means no separate assemblies for all your attributes, routing, helpers, etc.

Other big changes include no-compile updates (yes, no need to recompile while you’re developing – even for .cs files), Nuget for absolutely everything (including the .NET runtime!), and support for cross-platform hosting (no IIS or even Windows!).

Read more about the changes in Xinyang Qiu’s blog post.

Support for debugging Lambda Expression in Visual Studio

This is one of those “little things” that had a big effect on day-to-day work. In short, you can now write lambda expressions in all your debug windows.

Lambda expression in the Watch window

Lambda expression in the Watch window

Read more on Patrick Nelson’s blog post.

One new book and two new videos

For those of you who follow me on Twitter, you’d be aware of my recent activities.

Professional Team Foundation Server 2013

The achievement I’m most excited and proud about is that I’ve published a book – Professional Team Foundation Server 2013!

Professional Team Foundation Server 2013

Professional Team Foundation Server 2013

As the saying goes, it’s available at all good bookstores, but to help out here are some links to the book on Wiley’s websiteAmazon, The Book Depository, and Booktopia. Of course if you’d prefer an electronic copy, there’s always iTunes, Google Play, and the Kindle store!

Tech Ed New Zealand

I also recently presented a couple of sessions at Tech Ed New Zealand.

My first session introduced the awesome new Release Management for Visual Studio tool. It gives you a great way to deploy your applications in a repeatable and predictable way. It’s great for organisations that care about stable releases, governance, and deploying their applications quickly.

My second (and favourite) session was about the many extensibility points available to you in Team Foundation Server. This is a topic I became interested in while writing the Extensibility chapter in the book. I was amazed by how many opportunities there were to extend and customize Team Foundation Server. With the release of the REST APIs early this year, those opportunities grew even further. I’m really excited about the possibilities this opens up!

If you’re interested in integrating Team Foundation Server or Visual Studio Online with your own applications, this session is definitely worth a watch.

Getting Git Commits with the VSO REST API

(Guest post by Drew Robson – our SSW TimePRO product lead)

​​​​​​​​With Visual Studio Online now supporting Git, ​​​​more developers are changing their source control repositories. What happens if an application you developed relies on the TFS Client Object Model to get information out of source control (e.g. changeset comments) and the developers start using Git?​

That’s where the new Visual Studio Online REST APIs come in. You can get a list of commits from your VSO Git repository with only a HTTP request.​ Let’s look at how to do this with C# and Visual Studio 2013. The below example gets all the commits for a particular user.

(This is based on Get started with the REST APIs and VSO Integration Reference)

You will need a method to execute the HTTP GET request.

private async Task<string> Get(HttpClient client, string url)
    var result = string.Empty;

    using (HttpResponseMessage response = client.GetAsync(url).Result)
        result = await response.Content.ReadAsStringAsync();

    return result;

And a method to set up the authentication and urls.

public async Task<IEnumerable<Commit>> GetGitCommits(string username, DateTime from, DateTime to)
    var result = new List<Commit>();

    using (var client = new HttpClient())
        if(_settings.AuthType == AuthType.Basic.ToString())
            var version = "api-version=1.0-preview.1";

            // Set up auth
            var parameters = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", _settings.Username, _settings.Password)));
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", parameters);

            // Get Git repositories
            var url = string.Format("{0}/{1}/_apis/git/repositories?",
            var response = await Get(client, url + version);
            var repositories = JsonConvert.DeserializeObject<RepositoryCollection<Repository>>(response);

            // Get Git commits
            foreach (var r in repositories.Value)
                url = string.Format("{0}/{1}/_apis/git/{2}/repositories/{2}/commits?committer={3}&fromdate={4}&todate={5}&",

                response = await Get(client, url + version);
                var collection = JsonConvert.DeserializeObject<CommitCollection<Commit>>(response);
                if (collection != null
                    && collection.count > 0)

    return result;

What about the classes RepositoryCollection, Repository, CommitCollection and Commit? These are the class representations of the JSON responses. See the SSW Rule Do you know how to easily get classes from a JSON response? to generate them.

Release Management for the Enterprise (with videos!)

Team Foundation Server has been a great tool for managing your development projects for some time. But while it’s been a great source control and work item management tool, it’s always lacked a bit in terms of managing the deployment cycle of your application.

Too often, deployments rely on Frank from the Infrastructure team studiously following a word document to make sure every step has been checked off. It’s error-prone to say the least.

There are two great tools that are changing that. Both give you highly repeatable and automated deployments, reducing the risk of an outage.

The first is a new tool from Microsoft called Release Management for Visual Studio 2013. Yes, that’s a long name. The product has been around for a while under a different name (InRelease), but was recently acquired by Microsoft to improve this area of their offering. It’s closely-integrated with Team Foundation Server and gives you the ability to control your releases and know exactly who did what. It’s great for enterprises.

If you’re lucky enough to be at Tech Ed New Zealand this year, I’ll be speaking about it on Wednesday afternoon. I’m yet to hear about Tech Ed Australia, but I’ll let you know when I know :).

The other tool is one I’ve been using for a while called Octopus Deploy. It gives many of the same benefits as Release Management, but isn’t as closely tied to the Microsoft infrastructure. That means you can use it to deploy nearly anything from nearly any source (not just TFS).

I’ve spoken about Octopus Deploy a couple of times in the past few months, so rather than go through the features on this blog, I’ll just show you the videos.

For a quick introduction, watch my conversation with another SSW developer and MVP, Danijel Malik, about Octopus Deploy:

I also presented at DDD Melbourne recently about Octopus Deploy, and why it’s a great tool to stop you “deploying like an idiot”:

I hope you enjoy the videos, and I’m looking forward to talking about the other great tool, Release Management, at Tech Ed NZ.

TFS Maintenance – cleaning up after yourself

It’s really important to have a TFS Master as part of your team.  As the TFS Master at SSW (amongst other things), I’m constantly looking out for best practices, improvements we can make, and problems we can avoid. In particular, we like to keep our Team Foundation Server nice and clean, making sure the devs have no abandoned changes or workspaces.

Periodically, I’ll send out an email to anyone who has files they haven’t checked in for a while as per our “Do you have a report to see who has not checked in?” rule. What I often hear back is that the files have been checked out in a workspace that no longer exists. In other words the files were edited, not checked in, then the computer was rebuilt or replaced.

So what can you do?

The best option is to remove any workspaces you no longer have access to. There are two ways to do this:

  1. Use the awesome Team Foundation Sidekicks product
  2. Use the Workspaces tool in Visual Studio


Option 1 – Using Team Foundation Sidekicks

  1. Download Team Foundation Sidekicks 5.0
  2. Connect to your Team Foundation Server
  3. Use the Workspaces Sidekick to find and remove any workspaces you no longer use

Using Team Foundation Sidekicks to remove Workspaces

Using Team Foundation Sidekicks to remove Workspaces


Option 2 – Using the Workspaces tool in Visual Studio

  1. In Visual Studio, go to File | Source Control | Advanced | Workspaces…
  2. Check the Show remote workspaces checkbox to see workspaces on every computer
  3. Select and click Remove for the workspaces you no longer use

Opening TFS Workspaces in Visual Studio

Opening TFS Workspaces in Visual Studio

The Workspaces Dialog in Visual Studio

The Workspaces Dialog in Visual Studio


Clearing out these abandoned workspaces will let the TFS server know you aren’t using those workspaces any more. Best of all, your TFS Master will be less likely to send out angry emails!

I’ve been awarded a Microsoft MVP in Visual Studio ALM!

Microsoft MVP logoA few days ago I received an email from Microsoft informing me that I’d been awarded an MVP for Visual Studio ALM.

While my participation in the dev community has been purely for the enjoyment and love of what I do, this has been a career goal of mine and something I’ve been working towards for a few years.  I’m super excited and humbled to have been awarded alongside many of the best guys and girls in the field.

For those unfamiliar with what this award means, the best information can be found at, but here’s a quick quote from the site:

The Microsoft Most Valuable Professional (MVP) Award is our way of saying thank you to exceptional, independent community leaders who share their passion, technical expertise, and real-world knowledge of Microsoft products with others. … Today, there are more than 4,000 MVPs worldwide. They represent more than 90 countries, speak over 40 languages, answer more than 10 million questions a year, and are awarded in almost 90 Microsoft technologies—reflecting the breadth of our products and our global communities.

Thanks must go to the awesome guys and girls I work with at SSW for their support, as well as to the many enthusiastic developers I interact with as an organiser of the Brisbane .Net User Group and DDD Brisbane.

Here’s to a great 2014!


How to change a Work Item’s type in Team Foundation Server

One of the most frequent questions I get asked when training a new team in TFS is how to change a Work Item’s type.  If the team is coming from Jira, I can almost guarantee I’ll get asked that question.

While you can’t change the type of an existing Work Item with Team Foundation Server 2012 and 2013, there’s a simple workaround that achieves the same result.

How to change a Work Item’s type

First, open the Work Item in Team Web Access and click this button: Copy Work Item Button

The Copy Work Item button in Team Web Access

The Copy Work Item button in Team Web Access

In the dialog, choose the Work Item type you want to change it to, and click OK.

Choose the Work Item Type

Choose the Work Item Type

Finally, go back to the original Work Item and change its state to an appropriate “closed” state. If you really want to remove all traces of the original work item, you can use the command line “witadmin destroywi” tool. I don’t recommend this!

You can perform the same tasks from within Visual Studio by right-clicking on the work item and choosing “Create Copy of Work Item”.

Visual Studio - Copy Work Item

Visual Studio – Copy Work Item

It’s a deep copy

If you look at the new Work Item, you’ll see that all of the detail and links have been copied across where there’s a corresponding field in the new type.

All related Work Items are linked, and even links to Changesets that applied to the original Work Item are copied.

Deep Copy of Work Items

Deep Copy of Work Items

This tip can be really useful for Product Owners who like to create “bugs” that don’t fit your bug definition and are really new requirements.

So there you have it, a way of “changing” the type of a Work Item that should please those Jira converts!

The new, shiny goodies in Visual Studio and TFS

I recently spent a couple of weeks speaking at Tech Ed Australia and Tech Ed New Zealand with Adam Cogan.  You can see recordings of the Tech Ed Australia sessions or the Tech Ed New Zealand ones on Channel 9 if you want the full presentation.

In this post, I wanted to specifically point out a few of my favourite Visual Studio ALM features from our sessions.

Visual Studio IDE – Peek

When I’m reading code, I’m used to jumping around between files to find definitions and usages using key combinations like F12, Ctrl+F12Shift+F12, Ctrl+-, and Ctrl+Shift+-.  Peek adds yet another key combination – Alt+F12 – to the mix, and it’s an awesome addition.

When your cursor is in the right place, just hit Alt+F12, and the definition will expand below.  Even better, you can edit it inline!  Just hit Esc when you don’t want to see it any more.

Visual Studio 2013 - Peek

Visual Studio 2013 – Peek

This small thing has already saved me so much time while coding.

Visual Studio IDE – Code Lens

When you open a project in Visual Studio 2013, you’ll notice a couple of additional bits of text above things like class methods and properties.  These code lens helpers let you see at a glance, some really important information about the method or member below.

You’ll see:

  • References
  • The number of tests that touch this code
  • TFS information about this code including recent changes and associated work items

Hovering over these bits of text (or using shortcut keys Alt+1, Alt+2, etc.) expands this information and gives you the opportunity to run tests or navigate to code, TFS changesets, or work items.

Visual Studio 2013 - Code Lens

Visual Studio 2013 – Code Lens

This is incredibly powerful.  You can see immediately how this code is being used and what has happened to it. without needing to leave your code.

Team Foundation Service – Test Case Grid View

This feature is brand new (only a couple of days old) so we didn’t get a chance to announce it at Tech Ed Australia.  A demo will be in the Tech Ed New Zealand video when it’s live.

There’s a new option in the Test hub in Web Access where you can change the view of your Test Cases to “Grid”.  Just click on the View item on the right.

Team Foundation Service - Test Grid View

Team Foundation Service – Test Grid View

Team Foundation Service - Test Grid View

Team Foundation Service – Test Grid View

What’s amazing about this view is that you can paste straight from Excel!  If that doesn’t make your testers happy, nothing will!

Team Foundation Service – Team Room

The addition of a team room to TFS adds some awesome collaboration features to Web Access.  It’s particularly useful for remote teams (which I work in a lot).  Rather than just a chat room though, it can feed you information about your project.  You can see build completions, changes to code, and updates to Work Items.

Team Foundation Service - Team Room

Team Foundation Service – Team Room

The faster release cadence of the ALM team has meant much faster additions to Team Foundation Service. If I can suggest one thing, it’s to move your ALM process onto the cloud because there’s much more to come!

There are many more great features in VS and TFS 2013.  I encourage you to take a look.

VS 2013 – Packed Rooms at Tech Ed Australia and New Zealand

Developers are very excited to get their hands on Visual Studio 2013!

I’ve spent the last couple of weeks speaking at Tech Ed Australia and Tech Ed New Zealand with Adam Cogan.  We’ve been demonstrating the awesome new features in Visual Studio 2013, as well Team Foundation Server and Microsoft Test Manager.

Watch the recordings of the Tech Ed Australia sessions on Channel 9.

Note: At time of writing, one of the videos is unavailable. It should be online for viewing by midday 13th September.

Update: Both videos are available now

Channel 9 - Tech Ed Australia - My Sessions

Channel 9 – Tech Ed Australia – My Sessions

The rooms at Tech Ed Australia were packed!  About 450 developers came to see the two sessions showing just how popular the Visual Studio ALM stack is.

In my next post, I’ll go through some of the highlights of the two sessions.

Next Page »