Sequence contains more than one element

Easiest way to debug Seed-method in EF Code-first migrations in Configuration.cs when running Update-Database

Reading Time: 4 minutes. This post describes the easiest way to debug the issues that may stop your Seed-method in Configuration.cs from going through. The solution here shows you, how you can get a little bit more information out of the process, without attaching the debugger (there’s another blog post for that!) Description Entity Framework’s code-first migration’s are a beautiful and easy way of managing database schema changes and populating some preliminary data there. Personally I also sometimes use the method for adding some enrichment to data or or […]

Applying Entity Framework’s Code-First Migrations against a Database in Azure by running Update-Database

Reading Time: 3 minutes. This post describes how to run Entity Framework’s code-first migrations against a database located in the Windows Azure. This is done by running Update-Database commandlet with suitable switches, see below. The problem and symptoms Okay, so you’re developing your MVC+EF cool web app with a database in Azure, and you’re using code-first migrations. Cool! What’s nice with code-first-migrations is the fact they are run automatically even in the cloud the next time your app is running (as long as you publish your app with that […]

entity-framework-logo

Fixing the error: “Column XX in table dbo.YY is of a type that is invalid for use as a key column in an index.”

Reading Time: 2 minutes. While using Entity Framework and code-first migrations, Entity Framework (EF) creates the indexes for you – but what if you need to create a custom one, explicitly based on certain field / column? Then you’ll have to tell EF which one to use as a key column. Usually, it’s easy – you just add the following annotation to the columns you’ll be using: [Index(“OfficialIds”, 1)] public int AreaId { get; set; } [Index(“OfficialIds”, 2)] public string EstateId { get; set; } (example stripped of extra code and other columns for clarity) And after adding the migration (Add-Migration… commandlet) you get […]

Entity Framework's Update-Database throwing an error

Fixing “An error occurred while updating the entries” while running code-first migrations in MVC 5 app

Reading Time: 5 minutes. This post describes an issue with EF’s code-first migrations, when mapping between DB’s DateTime (datetime2) and C#’s DateTime kind of fails, and results in Update-Database cmdlet failing. It’s more or less a prime example of a situation, where the error itself tells very little about the actual issue, and since debugging code-first migrations is kind of difficult (see the best tips for that here!), it’s cumbersome to investigate. Symptoms An exception of type ‘System.Data.Entity.Infrastructure.DbUpdateException’ occurred in EntityFramework.dll but was not handled in user code. I’ve […]