Use Sql Server named instance as local to avoid changing the connection strings

When working on a team project, we often come across the issue that the database connection string in the configuration file uses the default instance name, but our development database is actually using a named instance (see below)
Default or Named Instance
Default Instance
Named Instance

Some developers just create a default instance and continue on their work. Others would just change the configuration file or have a local copy of the configuration file to match their environment and move on.
There is another approach that we can use which would use less resources (in the case of another instance), and it is a lot more convenient than having to manage multiple configuration files. We can use named pipes configuration to change the pipe name from the instance name to the default instance.
Named Pipes Configuration
When connecting to the default instance, SQL Server uses the default pipe name of "\.\pipe\sql\query". A named instance uses a different pipe name as listed below:
Pipe Name (Default)
Pipe Name (Instance)

We can use SQL Server Configuration Manager to first enable the named pipes setting and update the pipe name. This can be done as follows:
Select SQL Server Network Configuration (see pic below for details)
  • Click protocols for (Instance Name)
    • Double click on Named Pipes
      •             Set Enabled to Yes
      •             Update the pipe name
      •             Apply the changes.
      •             Restart the SQL Server service - SQL Server (SQLEXPRESS)

Use Sql Server named instance as local

After making those changes, we can try to connect to our database using SQL Server Management Studio. We can try to connect to the database with both (local) and (local)\express for server name, and both connections should be successful.

Thanks for reading.


ASP.NET MVC 5 Passwords must have at least one non letter or digit

If you are trying to register a new user, and you are getting the error below:

MVC 5 Passwords must have at least one non letter or digit

The new ASP.NET MVC 5 web templates are using the Identity framework to do the validation and password policy. This message indicates that the password policy requires a special character. In the event that our password policy does not need such requirements, we can disable this constrain.

To disable this constrain, we can follow these steps:
  • Open the identity_config.cs files under the App_start folder
  • Search for the string manager.PasswordValidator
  • We should see this code snippet:

// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
  • Set the RequireNonLetterOrDigit = false
  • Save and compile the application

The password requirements should now be a bit more flexible, and the error should no longer be displayed.

I hope that helps.


This project references NuGet package(s) that are missing on this computer

This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  

When getting this error, Visual Studio indicates that a NuGet package is missing from your project references.  If you take a look at the project->references node, you will probably see that there are some missing libraries that have been added to the project via a NuGet package installation, and your system does not have the package.  These files may even be the core .Net libraries like System, System.Xml etc.

To address this problem, we can just follow these steps:
  • Select Solution Explorer
  • Right click on the Solution Name
  • Click Enable NuGet Package Restore

This tells Visual Studio that whenever it finds a missing package, it needs to download it. After this has been enabled, we need to reload each project that has the missing references. This can be done by closing and opening the solution which will reload all the projects or just unloading and reloading each specific project (right click on the project name to see the unload project option).

Another area to make sure that the Package restore feature is enabled is to look for the .nuget folder in the solution’s directory. There should be a Nuget.targets file which manages the restore.

I hope this helps.


Add Data Annotations to Entity Framework Models with Metadata or Buddy Classes

When using code generation (ORM), the model classes are automatically created by the tools.  We usually try to edit the class and add annotations such as field requirements, formatting and messages. The problem with this approach is that when we need to add properties to the model class, we will need to re-generate the class and all the data annotations will be wiped out which create extra work for us.

A quick way to address this challenge is to create a Metadata or Buddy class that provides all the annotations that we need. We can then add an attribute to a partial class with the same name as the class that was generated to indicate that there is another class that provides the annotations on its behalf.   We can now take a look at an ASP.NET MVC project which can be loaded from the source code link at the end of this article.

Class Generated by Entity Framework (Database first approach)

We first create a simple table with the following definition:

CREATE TABLE [dbo].[Car] (
    [Id]    INT           IDENTITY (1, 1) NOT NULL,
    [Make]  NVARCHAR (50) NOT NULL,
    [Model] NVARCHAR (50) NOT NULL,
    [Trim]  NVARCHAR (50) NULL,

Note: This table is created using the .mdf files under the app_data folder.

We now can use EF by adding an ADO.Net Entity Data Model to create the model from our database which generates the following definition (see Models/car.edmx file). Do not forget to compile after the table has been imported.

namespace og.samples.aspnet.MetaDataClass.Models
    public partial class Car
        public int Id { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public string Trim { get; set; }        

As we can see, there are no annotations on this model. Since this is a generated class, we would like to add our annotation class instead of changing it. We need to do this before we generate the views.

New Partial and Metadata Classes

The steps to add our metadata class are the followings:

1)      Add a new class under the model folder
a.      Name it  car.metadata.cs
2)      Create another class that contains all the data annotations.
a.      This is not a partial class
b.      This is a sealed class as there is no need to instantiate it
3)      Add a new partial class with the same name as the ORM class
a.      Add the [MetadataType] attribute to the class that was just created
b.      Set the type to the class that has the annotations.
4)      Compile

The code should now look as follows:

namespace og.samples.aspnet.MetaDataClass.Models
    /// <summary>
    /// partial class definition to associate the ORM generated class
    /// NO NEED to add the properties here.
    /// </summary>
    public partial class Car

    /// <summary>
    /// Buddy Class or Data Annotation Class
    /// Add the properties here with the associated annotations
    /// </summary>
    internal sealed class CarAnnotation
        [Required(ErrorMessage="{0} is required")]
        [MinLength(3,ErrorMessage="{0} should have three or more letters")]   //kia
        public string Make { get; set; }

        [Required(ErrorMessage = "{0} is required")]
        public string Model { get; set; }
We are making Make and Model required. In addition, we are making the Make to have a minimum of three characters.

Create Controller and Views

We now just need to add our controller and views by adding a controller item under the controller folders and selecting the following properties:
  1. MVC 5 with controller with views, using Entity framework
  2. Enter the following settings and compile the project

 Cars Create View (see sample project)

Run the application and select Run Demo under the Metadata classes section.

This is how the view should look. Press Create for the validation to take place as shown below.

With the above view, we can show that our data annotation validations are shown when the user does not meet the input requirements.


With this approach we can show that we can still enable the use of ORM tools to generate the models and continue to support our application specific data annotation requirements without the concern of losing any information.

Code Sample

Available at:  GitHub  (see Dev Branch for latest changes)