Closed piotrkantorowicz closed 8 months ago
The behavior of Single, SingleOrDefault, and their async implementations throw exceptions if the query made has more than one element. It means that your filter query is not specific enough to gather at most 1 result. Consider using FirstOrDefault(Async), update your query, or update your database relationships to ensure the query guarantees at most 1 result.
@chadmobrien, I aware about how SingleOrDefault() works and I tried also used FirstOrDefaultAsync() with the same result. In case when I use ToListAsync() request has been frozen and looping on BudgetPermission constructor.
I also tried configure this as HasMany instead of Owned enties and it working fine with single entity (BudgetPermission) but behaved exactly in the same manner when I used Include (Permissions).
also used FirstOrDefaultAsync() with the same result
Are you saying you got System.InvalidOperationException: Sequence contains more than one element
with FirstOrDefaultAsync()? That seems highly unlikely - please check exactly what it is you're trying. Otherwise, I think we need an actual runnable, minimal code sample (and not partial snippets) in order to help further.
@roji, Correct, same error in both cases SingleOrDefaultAsync()/FirstOrDefaultAsync(). I prepared basic example here - https://github.com/piotrkantorowicz/EfCore.Investigation
@piotrkantorowicz Since the types your are converting to are reference types, they will use reference comparison by default. Either implement value semantics for non-entity types, or use a value comparer.
@ajcvickers, thanks for help. I was pretty sure that my typed ids were created as records and have value comparison built in.
Hi guys,
I have a problem with joining two simple entities, because of infinity loop I guess (observed during debugging).
here is an objects and its entity types configurations:
When I fetching a single object in SingleOrDefaultAsync() clause this query has been produced and it also throws exception:
SELECT t."Id", t."BudgetId", t."OwnerId", p."BudgetPermissionId", p."Id", p."ParticipantId", p."PermissionType" FROM ( SELECT b."Id", b."BudgetId", b."OwnerId" FROM "BudgetSharing"."BudgetPermissions" AS b WHERE b."Id"::uuid = @__filter_Id_0 LIMIT 2 ) AS t LEFT JOIN "BudgetSharing"."Permission" AS p ON t."Id" = p."BudgetPermissionId" ORDER BY t."Id", p."BudgetPermissionId"
EF Core version: Database provider: Postgres Target framework: NET 8.0
Could you explain me please what I doing wrong?