© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

❔ EF Core 7 - FromSql join with same column names

This code produces ArgumentException -> An item with the same key has already been added. Key: CustomerId.
Is there a way to fix this query? I know that I can remove the "i"."CustomerId" and it will work but imagine that Customer and Invoice entities have property with the same name this will cause the same error
Customers.FromSql($"""
SELECT "c"."CustomerId", "c"."Address", "c"."City", "c"."Company", "c"."Country", "c"."Email", "c"."Fax", "c"."FirstName", "c"."LastName", "c"."Phone", "c"."PostalCode", "c"."State", "c"."SupportRepId", 
"i"."InvoiceId", "i"."BillingAddress", "i"."BillingCity", "i"."BillingCountry", "i"."BillingPostalCode", "i"."BillingState", "i"."CustomerId", "i"."InvoiceDate", "i"."Total"
FROM "Customer" AS "c"
LEFT JOIN "Invoice" AS "i" ON "c"."CustomerId" = "i"."CustomerId"
ORDER BY "c"."CustomerId"
""").AsNoTracking().ToList().Dump();
Customers.FromSql($"""
SELECT "c"."CustomerId", "c"."Address", "c"."City", "c"."Company", "c"."Country", "c"."Email", "c"."Fax", "c"."FirstName", "c"."LastName", "c"."Phone", "c"."PostalCode", "c"."State", "c"."SupportRepId", 
"i"."InvoiceId", "i"."BillingAddress", "i"."BillingCity", "i"."BillingCountry", "i"."BillingPostalCode", "i"."BillingState", "i"."CustomerId", "i"."InvoiceDate", "i"."Total"
FROM "Customer" AS "c"
LEFT JOIN "Invoice" AS "i" ON "c"."CustomerId" = "i"."CustomerId"
ORDER BY "c"."CustomerId"
""").AsNoTracking().ToList().Dump();


Note: I am aware that I can just use LINQ. I want to know if there is a way to fix the FromSql approach.
Customers.Include(c => c.Invoices).Dump();
Customers.Include(c => c.Invoices).Dump();
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

EF Core
C#CC# / help
2y ago
❔ EF Core Adding "1" to Column Name
C#CC# / help
3y ago
✅ EF Core migrations with Docker
C#CC# / help
11mo ago
Calculated Properties with EF Core
C#CC# / help
2y ago