3

I am trying to update my edmx, RIGHT CLICK -> UPDATE MODEL FROM DATABASE. And each time i try to update error appear "An entry with the same key already exists

Can some buddy please help?

.enter image description here"

Muhammad Bashir
  • 179
  • 2
  • 2
  • 12
  • after so much research on internet, i found this too. But what is duplicate? how to handle it? – Muhammad Bashir May 19 '16 at 06:25
  • 3
    There are some hints you have to check: => Usualy you have two identical nodes in EntitySetMapping open your edmx file in a Text-Editor, it may won't display the dublicate to you from the designer – Cyril Iselin May 19 '16 at 06:51
  • Switch to Code first - see http://blogs.msdn.com/b/adonet/archive/2014/10/21/ef7-what-does-code-first-only-really-mean.aspx – Vojtěch Dohnal May 19 '16 at 07:02

2 Answers2

6

Sometimes the duplicate might not be shown in the edmx diagram, it could the code side. EF can get a little messy sometimes but not too good in dealing with those loop holes.

As an addition to Fernanda's suggestion, the cleanest way is to delete your edmx file and reconfigure the connection string again.

in case you don't want to do that, you can delete all tables & functions in your edmx, save it and see if the same error still occur. If the same error don't occur anymore, re-add everything and save it.

Koo SengSeng
  • 933
  • 3
  • 12
  • 31
  • Thank you for answer. I think this is the only solution. Founding duplicate key is almost impossible. I have to delete and regenerate it. – Muhammad Bashir May 19 '16 at 07:04
  • 13
    Thanks.. yes duplicates do not get shown. Using the tips from @cyril iselin, i was able to locate the duplicate. Approach I took was Do a find in file for " – Srini Apr 20 '18 at 04:36
  • 2
    Good advice from Srini. I had a duplicate and a find in file quickly found the duplicate. I removed it and problem gone. :) – RitchieD Oct 26 '18 at 14:19
0

Note that EDMX approach will be deprecated in Entity Framework 7:

“Update model from database” is a process that allows you to incrementally pull additional database objects (or changes to existing database objects) into your EDMX model. Unfortunately the implementation of this feature wasn’t great and you would often end up losing customizations you had made to the model, or having to manually fix-up some of the changes the wizard tried to apply (often dropping to hand editing the xml).

For Code First you can re-run the reverse engineer process and have it regenerate your model. This works fine in basic scenarios, but you have to be careful how you customize the model otherwise your changes will get reverted when the code is re-generated. There are some customizations that are difficult to apply without editing the scaffolded code.

So the recommended approach for EF 7 is to do reverse engineering of database into code, not into edmx, something called code second approach before.

Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105