Finding Entity Framework Key Duplication Errors using Sublime Text

By September 23, 2016.NET

Where would we be without source control for software projects? Probably lost in a sea of files named Copy1, Copy2, Copy2Final, and Copy2FinalFix2 or some such nonsense. Yikes! But even good source control and the best of intentions can still end you up having to merge branches. And once you’re at that point, there’s nothing to do but just Do. The. Merge. And get it over with. So what does Entity Framework have to do with merging files? Oh, I thought you’d never ask.

Merging problems with EDMX

Entity Framework was my first experience with ORMs (object-relational mappers), and for a while it was just about the only real choice out there. So I’m never surprised when it is the ORM in place for any established software project. And EF brings with it my favorite file to merge – the .edmx file. It’s a perfect setup for merging hell – a generated file with reordered elements that can change location on each generation, and a high probability for many changes for each feature set.

So, so many times I’ve seen a reasonable merge turn bad on a .edmx file, and Visual Studio is pretty much no help with the errors either. Here was one I experienced recently with EF5:

Unknown build error, ‘An item with the same key has already been added.’  

Do you know what would be nice? Knowing what the duplicate key is! But alas, the one piece of info I need isn’t there. And searching the great wild internet for solutions was even more frightening. One very long thread of many miserable souls like myself finally agreed that the only solution is to delete the entire model and recreate it from scratch. No no no no, please no. It’s a duplication, surely there must be a way to find it, right?

Using Sublime Text

Well, there is at least one way, and it does not need you to recreate the model. Actually I’ve used this method several times to find duplicated elements, and the trick is not in Visual Studio but to open the file in Sublime Text. I’m sure other editors can be used for this same purpose, but for me it is the best tool for this job. You’ll also want this add-on to highlight duplicates.

Select the entire file, and sort the lines using the Edit menu. You probably will not want to save these changes, by the way – this is purely investigative editing here. Now, whatever the duplicated key is, at least the lines will be together.


Now, use the Command Palette to find the Highlight Duplicates command and toggle the highlighting on.

2016-09-22-22_06_01-c__users_rachel_documents_visual-studio-2015_projects_sampleefproject_sampleefpr    2016-09-22-22_05_47-c__users_rachel_documents_visual-studio-2015_projects_sampleefproject_sampleefpr

Many lines will be highlighted as duplicates, and that’s expected. What you want to find is an EntityType line that is duplicated that shouldn’t be there. There it is! Now you can open the file back up in Visual Studio and find and remove the offending line. Success! You may now resume your regularly scheduled programming.


Did you learn something? Check out more of my latest posts for other useful tips and problem solving.

The following two tabs change content below.

Latest posts by Guest Posts (see all)