-
Notifications
You must be signed in to change notification settings - Fork 88
Analyzer to spot comparison with primitives in EFCore Linq selectors #684
Copy link
Copy link
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
In a Linq query across a collection I can do this without a problem
var x = list.Where(e => e.Id == 1).Single();However, the same query in EFCore will fail with an InvalidCastException and a message of 'Invalid cast from 'System.Int32' to 'Domain.Id'
To fix this, an explicit cast is required
var x = context.Entities.Where(e => e.Id == (Id)id).Single(); or
var x = context.Entities.Where(e => e.Id == Id.From(id)).Single(); These are difficult to spot at compile time as the Id and int types are equatable.
One way to prevent the runtime errors is to generate the implicit cast, but this allows bypassing the From method and introduces the potential to create invalid instances.
Originally posted by @rbanks54 in #502 (comment)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request