Entity framework edmx 更新失败。出现错误 "An entry with same key already exists."

Entity framework edmx fail to Update. Error appear "An entry with same key already exists."

我正在尝试更新我的 edmx,右键单击 -> 从数据库更新模型。每次我尝试更新错误时都会出现“具有相同密钥的条目已经存在

有没有哥们帮忙?

."

有时重复项可能不会显示在 edmx 图表中,它可能在代码端。 EF 有时会变得有点混乱,但在处理这些循环漏洞方面不太好。

作为 Fernanda 建议的补充,最干净的方法是删除您的 edmx 文件并再次重新配置连接字符串。

如果您不想这样做,您可以删除 edmx 中的所有表和函数,保存它并查看是否仍然出现相同的错误。如果不再出现相同的错误,请重新添加所有内容并保存。

注意 EDMX 方法 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.

因此,对于 EF 7,推荐的方法是将数据库逆向工程为代码,而不是 edmx,之前称为代码第二种方法。