Skip to content

{ Category Archives } C#

How to Fix MSBuild Error MSB4006

You may encounter an error which looks like this:
MSB4006: There is a circular dependency in the target dependency graph involving target "ResolveProjectReferences" [MyProjectName\MyProjectName.csproj]
…when running MSBuild from the command line.
This error happens when:

You run MSBuild on a machine with .NET 4.5 installed and
You build a project referencing a solution where the SLN file contains dependencies in [...]

Tagged , ,

Ad-hoc SQL/POCO Queries in Entity Framework 4.0

Since version 4.0, the Entity Framework has had the ability to query un-mapped data and project it onto POCOs using ad-hoc SQL.
Here, for example, is how we check the current SQL Server version:

internal class SqlVersionInfo
{
[...]

Tagged , ,

Testing Cross Cutting Concerns

So imagine you’ve been asked to implement the following requirement:
When a to-do list item is marked as complete, the CompletedOn date time property shall be set to the current time.
That doesn’t sound so hard to implement, but how can I test it? I don’t know precisely what the value of the CompletedOn property "should be" [...]

Tagged , , ,

Why Won’t Visual Studio Step Into This Code?

I helped another developer debug an interesting problem this morning. Let’s see if you can spot the problem. The code in question looked something like this simplified version containing only enough code to show the problem:

public void Execute()
{
DoStuff(); // breakpoint 1
}

public IEnumerable<Coordinate> DoStuff()
{
[...]

Tagged , ,

A Better View API for Grids in ASP.NET MVC

I’m writing a grid-independent interface for displaying data in ASP.NET MVC applications, and I would like your feedback on the API design.
In my last post, I discussed some of the problems with existing grid components for ASP.NET MVC. Actually, there are a couple more design issues which I forgot to mention in that post. I’ll [...]

Tagged , ,

One (MVC) Grid to Rule Them All

Imagine you’re starting a new project using ASP.NET MVC. Let’s say it’s a project which frequently requires displaying a list of records, like Google or Stack Overflow or an enterprise database application. Which grid should you use?
The obvious answer is, "I don’t know. I’m just getting started. Does it really matter, right now?" Don’t you [...]

Tagged , , , ,

How to Avoid Cut and Paste Code with ASP.NET MVC 2 Model Validation

In this post, I will demonstrate how to make your own model validation attributes in order to share common validations throughout an ASP.NET MVC application, and which support MVC 2’s client-side validation feature.
Validating a ZIP Code
As an example, consider a model for an address.

public class EditModel
{
public Guid Id { get; set; [...]

Tagged , , ,

Unit Testing Html Helpers for ASP.NET MVC

Html helpers for ASP.NET MVC are static extension methods, which frequently reference the ViewContext and  HttpContext. Combined, this can make unit testing a bit tricky. Let’s write a new Html helper using a test-first methodology. Let’s start with a prototype function:

        public static MvcHtmlString MyTable(this HtmlHelper helper, MyModel model, IDictionary<string, object> htmlAttributes)
        {
            return MvcHtmlString.Empty;
        }

I’ve added just enough code here to get the prototype to compile. Now let’s write [...]

Tagged , , , , ,

Troubleshooting Entity Framework Connection Strings

In an application which uses the Entity Framework, you may see the following error at runtime:
MetadataException: Unable to load the specified metadata resource
The cause of this error is a missing or malformed Entity Framework connection string. In particular, the cause of this error is the metadata parameter of the connection string. Let’s examine this more closely.
Which Config File?
Before [...]

Tagged , ,

Replacing Controller.Session in ASP.NET MVC: Is This Wrong?

Here’s some code I wrote:

public class MyBaseController : Controller
{
// …
public new ISession Session { get; private set; }
// ….
}

ISession is an interface type I wrote which exposes everything I store in the session at runtime. I use constructor injection to assign this to [...]

Tagged , ,

Bad Behavior has blocked 713 access attempts in the last 7 days.

Close