I am developing a small application. I am using ASP.NET 8 with FastEndpoints and EntityFrameworkCore with the Sqlite Provider as a backend.
I have an endpoint where I create a reservation. For the reservation I need the corresponding vehicle. In the request I have the VehicleId.
This is the code-snippet from the endpoint:
VehicleModel? requestedVehicle = await _database.VehicleModels.FindAsync(req.Vehicle);
if (requestedVehicle is null)
{
_logger.LogWarning("Could not find Vehicle {VehicleId}", req.Vehicle);
await SendNotFoundAsync(ct);
return;
}
Even tough the requested vehicle is in the database, I always get null
back. I tried capturing this in the following Screenshot. In the top-right you can see the database content with the id. On the lower you can see the request object with the requested vehicleId.
Debugger – Request (VehicleId) does exist in database
This happens even when I change from FindAsync
to await _database.VehicleModels.FirstOrDefaultAsync(x => x.Id == req.Vehicle);
Any ideas why this is happening?
This is my model class:
[StronglyTypedId(Template.Guid)]
public partial struct VehicleModelId {}
public sealed record VehicleModel
{
public VehicleModelId Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<ReservationModel> Reservations { get; set; } = new();
}
And this is my DatabaseContext:
public class DatabaseContext : IdentityDbContext<UserModel, IdentityRole<Guid>, Guid>
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { }
public DbSet<ReservationModel> ReservationModels { get; set; } = null!;
public DbSet<VehicleModel> VehicleModels { get; set; } = null!;
public DbSet<UserModel> UserModels { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<UserModel>()
.HasMany(u => u.ReservationsMadeByUser)
.WithOne(u => u.ReservationMadeByUser);
modelBuilder.Entity<ReservationModel>()
.HasKey(u => u.Id);
modelBuilder.Entity<ReservationModel>()
.Property(x => x.Id)
.HasConversion(x => x.Value, x => new ReservationId(x));
modelBuilder.Entity<VehicleModel>()
.HasKey(u => u.Id);
modelBuilder.Entity<VehicleModel>()
.Property(x => x.Id)
.HasConversion(x => x.Value, x => new VehicleModelId(x));
modelBuilder.Entity<VehicleModel>()
.HasMany(x => x.Reservations)
.WithOne(x => x.VehicleReserved);
}
}
And this is the request class I am using for the endpoint. The modelbinding works, I just wanted to include this:
public record CreateReservationRequest
{
[Required]
public DateOnly StartDateInclusive { get; set; }
[Required]
public DateOnly EndDateInclusive { get; set; }
[Required]
public Guid ReservedBy { get; set; }
[Required]
public VehicleModelId Vehicle { get; set; }
}
Note: I am using StronglyTypedId, but as you can see I added all the conversions
As described above, I already tried switching from FindAsync
to FirstOrDefaultAsync
as i thought there would be an error with the FindAsync method.
I also tried changing the Guid from lowercase to Uppercase (all possible permutations in the databse and the request) but also got no luck there.
kreisi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.