Error modifying DAL, System.ArgumentException, “An entry with the same key already exist”

最后都变了- 提交于 2019-11-30 17:20:44

问题


OK, Im totally stumped with this one. I may not have enough info to post here, but I dont even know where to start looking. I'm trying to "Update Model from database" on my DAL.edmx file. I included a field to a view that wasnt included prior. I tried refreshing, and then I tried renaming the view in the database and deleting the view from the DAL so I could re-add it. Both times I got

Next, for no reason I tried adding my renamed view into the DAL, got same exception. Manually deleting from DAL.tt does not help. Googled issue and only 2 non-relevant results. I dont know where to even start looking.

I didnt write it, but here is the source sql of the view (if it helps). The fact that EF wouldnt add the renamed view hints it might be with the SQL? The SQL runs 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: Created brand spanking new view with same SQL, went to add it by same method to DAL, same error.


回答1:


I had exactly the same issue. As i noticed, the issue appeared after merging of .edmx files with Subversion. Looking on .edmx file in text editor, i found one duplicated EntitySetMapping entry. After manually deleting the duplicate, the issue was solved! Hope this helps




回答2:


You probably have two identical nodes: EntitySetMapping. You should remove one and everything will be ok. Try to remove all the mappings for the view and add them again. If this doesn't work try to look in the Model Browser view and under Model/Entity Types. There could be some entities that were left during old migration and when you try to add a table with the same key your error occurs. Hope this helps ;]




回答3:


I had exactly the same problem, and found the clue to the solution in cedenbal's answer above - duplicate EntitySetMapping entries. Problem was: how to find it/them in an EDMX of almost 3Mb with 250+ tables. Solution was to (a) run a "Find All" in Visual Studio on "EntitySetMapping Name=" on files of type EDMX. This yielded a list of 250+ entries (as expected), but not in any kind of order where I could spot duplicates. So (b) cut and pasted the list into Notepad++, ran a macro to remove the chaff, leaving just the tablenames, (c) cut and pasted this list into Excel, and sorted it A-Z. Then (d) just eyeballed the list looking for duplicates. Found a whole section containing 8 duplicate ESMs! Removed them, saved the EDMX, reloaded the EDMX in Visual Studio, re-ran "Update from database", and bingo.




回答4:


remove/add was not suitable for my current project.

solved this problem with a one-time small program for finding duplicate mapping 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());
            }
        }
    }
}



回答5:


I dont know if this will help, because I dont know enough to make sense of it, but I happened to see that the partial class vw_BillingIssues.cs did in fact have the extra field in it. I did a solution-wide search for the term "vw_BillingIssues" and added the extra field to any list or collection that was missing it (pretty much everywhere else), rebuilt the solution and Success! I did notice somewhere it mentioned the underlying tables of the view did not have a primary key defined, but dont remember where I saw that.




回答6:


In my case the following solution helped:

Open .edmx file with XML Editor and try to find duplicated EntitySetMapping.

Duplicated EntitySetMapping:



来源:https://stackoverflow.com/questions/32783135/error-modifying-dal-system-argumentexception-an-entry-with-the-same-key-alrea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!