sachin srivastava sachin srivastava - 1 year ago 102
ASP.NET (C#) Question

Remove duplicate entries in database using entity framework

I am using Entity Framework 6.1 code first . I am working on a product in which the release version of the product there there are duplicate entries in a UserName column in the UserProfile table. To remove this bug I am using the Index annotation on the UserName field.

class UserProfile
string UserName;
string Id;

so that now the duplicate entry problem goes away. But I dont have backward compatibility now. As in the customers who are already using the release product and have duplicate entries in their database cannot use their existing database.

I have created a migration using entity framework and it updates the database like this

public partial class UserConcurrencyFix : DbMigration
public override void Up()
CreateIndex("dbo.UserProfiles", "UserName", unique: true);
public override void Down()
DropIndex("dbo.UserProfiles", new[] { "UserName" });

This does not take care of existing databases that already have duplicate entries. Is there a way I can remove duplicates from databases that already being used by the users and have duplicate entries?

Answer Source

In your Up migration method, before the index is added. Execute a script using the Sql() method. This can be any script you want, so just create one that does the desired deduplication and you're all set.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download