Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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.

like image 986
Darkloki Avatar asked Sep 25 '15 13:09

Darkloki


2 Answers

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

like image 190
cedenbal Avatar answered Nov 09 '22 18:11

cedenbal


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());
            }
        }
    }
}
like image 37
bob217 Avatar answered Nov 09 '22 20:11

bob217