I am facing an issue where I cant fetch an entity when using .FirstOrDefaultAsync() with a GUID as the primary key.
If I fetch all entries first and then run .FirstOrDefault it works.
The entity in question also exists when inspecting the DbContext.

Using .NET 8.0 with Entity Framework 9.0.0 and Sqlite
public async Task AddClaim(User user, Guid claimId)
{
// returns null
var claim = await context.Claims.FirstOrDefaultAsync(x => x.Id == claimId);
// this works
var claims = await context.Claims.ToListAsync();
var localClaim = claims.FirstOrDefault(x => x.Id == claimId);
// this throws UniqueContrain exception
context.Claims.Add(new Domain.Entities.Claim() { Id = claimId });
// ...
}
public class Claim
{
public Guid Id { get; set; }
public string Name { get; set; } = null!;
public string Value { get; set; } = null!;
public ICollection<User> Users { get; set; } = null!;
}
>Solution :
SQLite can store Guid values as TEXT or BLOB
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Claim>()
.Property(c => c.Id)
.HasConversion(v => v.ToString(), v => Guid.Parse(v));
}
This should work.