Column '' in table '' is of a type that is invalid for use as a key column in an index.

I've tried to create a simple (unique) constraint across multiple columns:

However I was greeted by: Column 'Name' in table 'dbo.UserLocations' is of a type that is invalid for use as a key column in an index. This can be fixed by adding: [DataType("VARCHAR")] and [MaxLength(40)] on the string. The point is that MaxLength...

ASP.NET Automatic Migrations

Understanding migrations can be a bit tricky. Hopefully, this article will resolve at least some of the questions you might have. First things first - What are migrations? Well... If you built your website using first code approach every time you change your model you need in order to keep the database in sync. There...

Required attribute not triggering an exception on SaveChanges()

This was a very interesting (and annoying at the same time). I was writing some integration¬†tests and it turns out that the [Required] attribute doesn't trigger an exception on SaveChanges().  

No exception raised. Instead, we can make the required property nullable.

This time we get: System.Data.Entity.Validation.DbEntityValidationException. I did some digging and it...

Removing Entry from a Database using Entity Framework

Hmm... Most of the tutorials on the internet seem to focus on how to remove these objects from DataContext as oppose to DbContent. In a Code First approach I don't really use DataContext, so I had to remove the object via DbContext and this is how it's done:

Seems to be pretty simple...

Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths

Today, when I was working with the Entity Framework I got a really annoying error: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths I discovered several ways of getting rid of this exception. The fastest of which was:

As I am building only a prototype: data integration...

Populating Database with Random Data

Generating large set of data can be potentially problematic. The prototype I am currently working on needs to presentable, so putting names like "ASDF" makes no sense and doesn't look particularly good.   I used:¬†http://listofrandomnames.com/ to populate my database with random names. Below you will find some C# ready code if you want to use...

Accessing DbContext From a Different Project

Today, I discovered in interesting pitfall when accessing the database from different projects (inside one solution). I have defined connectionString in the Web.config file. To my dismay, this was not sufficient if you want to use your data context in different projects. Specifically, I was greeted by "Connection could not be opened" error. The inner...