I ran across this issue when using **Entity Framework 5.0 (RC)** in **.NET 4.5 (RC)** and I thought I would share it to try and help others.
The issue I was having was that I was trying to use the new **Database Migrations** to seed data in my table. This worked fine, but my data would get seeded everytime, despite the fact I was using the **AddOrUpdate** method on the **DbSet**. As the **AddOrUpdate** method suggests, the data should be added or updated.
This resulted in the seeded data being added into the table everytime when calling **Update-Database** from the **Package Manager Console.**
The code I had was like this:
The department entity had two columns:\
- Id - int - PK
- Name - varchar(250)
As you can see, I am telling **EF** to Add or Update 3 departments, but crucially I am only supplying a **Name** field. This is because the **Id** column is an identity column and I really have no idea what this value would be. Without the Id column being supplied **EF** does not recognise these departments as already existing. Therefore it adds new departments each time **Update-Database** is called.
My first thought was to mark the **Name** column as unique with a **DataAnnotation**, but at the time of writing, Unique columns are not supported by **EF**.
Fortunately, the **AddOrUpdate** method supplies some overloads which allows you to supply a **Func** expression** **as the first parameter which allows you to give **EF** an expression to evaluate whether an Add or an Update should be run. My final code looks as follows:
This now works as expected as the comparison is performed on the **Name** column only.