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.

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.

 

Fix: The located assembly's manifest definition does not match the assembly reference

Got the below error message ever?

The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I know how frustrating that is.

One of my team member added JSON.NET via nuget and all of a sudden all other devs start getting the above error message while invoking any methods from JSON.NET. I tried a dozen workaround that I got while searching for solution but none of them worked. After careful, time consuming analysis I figured out the issue. Our project was referring few PCL projects which was provided by client. All those projects were referring JSON.NET v6.0.3. Yay! That’s an old stable working client library. I uninstalled JSON.NET v9 and installed v6.0.3 which fixed the issue.

However, what still surprises me is why the dev who added v9 library not getting any error while others devs are getting the error? Weird.

Thanks.