There is already an open DataReader associated with this Command which must be closed first

Last week, I deployed one of the Web API website that uses Entity Framework to production server. In the publish wizard, I generated database connection string for production so that it'll connect to production database when deployed. To my surprise, few API calls resulted in an error :

System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

Yup, the solution was to include MultipleActiveResultSets=True; in connection string. In fact, this was there in the connection string of my development database but show how not included while generating connection string of production database.

Hope this helps.

Fix: Login failed for user IFC\WIN5177$ in ASP.NET MVC

We recently moved an ASP.NET MVC website to a new provider and found out that the MVC application is no longer connecting to SQL Server database. That was surprising because it was up and running with previous provider without any issues.

Below is the exception I got while connecting to SQL Server:

Login failed for user 'IFC\WIN5177$'.

That took quite sometime to figure out what was the issue. And, I finally fixed by setting Integrated Security=False; in the connection string,

Hope this helps.

Thanks.

Add GUID as primary key column in code-first migration

Last week, I was migrating an old ASP.NET MVC project and as part of that I decided to replace the identity column from auto-incremented numeric value to GUID values which looks more professional.

Below are the changes that you need to make to achieve this.

// in your model class.
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

 

Run, Add-Migration command which generates your migration class and make below change:

// in your migration class.
AlterColumn("dbo.LoLChampionPickeds", "Id", c => c.Guid(nullable: false, identity: true, defaultValueSql: "newsequentialid()"));

That's it. Now do an Update-Database and your Id field will start using GUIDs.

Thanks.