EfCore doesn’t find correct object in database

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.

New contributor

kreisi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật