Error modifying DAL, System.ArgumentException: "A record with the same key already exists"

OK, I'm completely stumped with this. I may not have enough information to publish here, but I don’t even know where to start. I am trying to "Update Model from Database" in my DAL.edmx file. I included the field in a view that was not previously included. I tried updating, and then tried renaming the view in the database and deleting the view from the DAL so that I could re-add it. Both times I got

enter image description here

Further, for no reason, I tried to add my renamed view to DAL, received the same exception. In manual removal from DAL.tt does not help. The problem is with Googled and only 2 irrelevant results. I don’t know where to even start looking.

I did not write it, but here is the original sql representation (if it helps). The fact that EF will not add renamed hints for the view, can it be with SQL? SQL works fine in mngmnt studio.

SELECT ID, IssueID, IssueTypeID, IssueText, IssueCreateDate, WeekendDate, CustomerName, Employee, CONVERT(DECIMAL(6, 2), AdjustedTotalRHours, 101) AS AdjustedTotalRHours, AdjustedTotalOHours, AdjustedTotalRHours + AdjustedTotalOHours AS Hours, InvoiceNumber, AdjustedInvoiceAmount, COALESCE ((SELECT SUM(InvoiceAmount) AS Expr1 FROM TrendingDataFinal AS I1 WHERE (InvoiceNumber = T1.InvoiceNumber) AND (CompanyID = T1.CompanyID) AND (CalType = 'F') AND (Aident = T1.Aident)), 0) AS TotalInvoiceAmount, InvoiceDate, ROUND(DATEDIFF(DAY, InvoiceDate, GETDATE()), 0) AS DaysOutstanding, Notes, Aident, EINC, IsClosed, CompanyID, (SELECT COUNT(ne.EntryID) AS Expr1 FROM Madison.Notes.Note AS n INNER JOIN Madison.Notes.NoteEntry AS ne ON n.NoteID = ne.NoteId WHERE (n.Key1 = T1.InvoiceNumber)) AS HasNotes, COALESCE ((SELECT TOP (1) CompanyName FROM ReportingCompanies AS I1 WHERE (CompanyId = T1.CompanyID)), '') AS CompanyName, BranchName, PayStatus FROM BillMan_ReportStage AS T1 

Any suggestions would be appreciated.

UPDATE: The created brand spanks a new look with the same SQL, went to add it with the same method to DAL, the same error.

+5
source share
6 answers

I don’t know if this will help, because I don’t know enough to understand it, but it seemed to me that the partial class vw_BillingIssues.cs really has an extra field. I did a search for a solution in the broad sense of the word "vw_BillingIssues" and added an additional field to any list or collection that was missing (almost everywhere), rebuilt the solution and success! I noticed that somewhere he mentioned that the primary key is not specified in the base tables of the view, but I do not remember where I saw it.

0
source

I had exactly the same problem. As I noticed, the problem arose after merging .edmx files with Subversion. Looking at the .edmx file in a text editor, I found one duplicate EntitySetMapping entry. After manually deleting the duplicate, the problem was resolved! Hope this helps

+11
source

You probably have two identical nodes: EntitySetMapping. You must delete one and everything will be fine. Try to remove all mappings for the view and add them again. If this does not work, try looking in the "Model Browser" view and in the "Model Types" or "Object Types" section. There may be some objects that were left during the old migration, and when you try to add a table with the same key, your error occurs. Hope this helps;]

+2
source

I had exactly the same problem and I found the key to the solution in the cedenbal answer above - duplicate EntitySetMapping entries. The problem was this: how to find him / them in EDMX with almost 3Mb with 250+ tables. The solution was as follows: (a) run "Find All" in Visual Studio on "EntitySetMapping Name =" in files of type EDMX. This gave a list of 250+ entries (as expected), but not in any order where I could detect duplicates. So, (b) we cut and paste the list into Notepad ++, run the macro to remove the chaff, leaving only the tablenames tags, (c) cut and paste this list into Excel and sort it AZ. Then (d) just scanned the list looking for duplicates. Found a whole section containing 8 duplicate ESM! Deleted them, saved EDMX, restarted EDMX in Visual Studio, restarted "Update from the database" and bingo.

+2
source

delete / add is not suitable for my current project.

solved this problem with a one-time small program to find duplicate matching entries

 using System.Collections.Generic; using System.IO; using System.Linq; namespace EntityModelHelper { class Program { static void Main(string[] args) { // path to edmx file var filesPath = @"SamePath\Model.edmx"; var lines = File.ReadAllLines(filesPath); var searchNames = new List<string>(); foreach (var line in lines) { var searchString = "<EntitySetMapping Name="; if (line.Contains(searchString)) { var tmp = line.Substring(line.IndexOf(searchString) + searchString.Length+1); var searchName = tmp.Substring(0, tmp.IndexOf("\"")); searchNames.Add(searchName); } } foreach (var duplicateName in searchNames.GroupBy(x => x).Where(x => x.Count() > 1)) { Console.WriteLine(duplicateName.First()); } } } } 
+1
source

In my case, the following solution helped:

Open the .edmx file with the XML Editor and try to find a duplicate of EntitySetMapping .

Duplicated EntitySetMapping :

duplicated EntitySetMapping

0
source

Source: https://habr.com/ru/post/1232276/


All Articles