'Getting an invalidcastexception when trying to retrieve data

When running the get method to return a view populated with details, I get the exception saying:

System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.DateTime'.'

The code code that does it is this:

 public IActionResult GetBookings()
    {
        List<Booking> list = new List<Booking>();

        foreach(var b in _airPortContext.Booking) // <--- Exceptions comes on this line
        {                
            list.Add(b);
        }

        return View(list);
    }

I am not sure why it's trying to cast the object to a date type

My model looks like this:

    public class Booking
{
    public int BookingID { get; set; }
    [Display(Name = "Class type")]
    public int ClassLevel { get; set; }
    [Display(Name = "From")]
    public string FromDestination { get; set; }
    [Display(Name = "To")]
    public string ToDestination { get; set; }
    [Display(Name = "From date")]
    public DateTime FromDate { get; set; }
    [Display(Name = "To date")]
    public DateTime ToDate { get; set; }
    [Display(Name = "Class")]
    public ClassType TravelClass { get; set; }              
}

------------------Update------------------

       protected override void BuildModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .UseIdentityColumns()
                .HasAnnotation("Relational:MaxIdentifierLength", 128)
                .HasAnnotation("ProductVersion", "5.0.0");

            modelBuilder.Entity("Airport.Models.Booking", b =>
                {
                    b.Property<int>("BookingID")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int")
                        .UseIdentityColumn();

                    b.Property<int>("ClassLevel")
                        .HasColumnType("int");

                    b.Property<DateTime>("FromDate")
                        .HasColumnType("datetime2");

                    b.Property<string>("FromDestination")
                        .HasColumnType("nvarchar(max)");

                    b.Property<DateTime>("ToDate")
                        .HasColumnType("datetime2");

                    b.Property<string>("ToDestination")
                        .HasColumnType("nvarchar(max)");

                    b.Property<int>("TravelClass")
                        .HasColumnType("int");

                    b.HasKey("BookingID");

                    b.ToTable("Booking");
                });

-------------Update--------------

enter image description here



Solution 1:[1]

This is error is likely caused by a mismatch between the types in your database, and the types of your model.

Note : this can have many causes, depending on whether this is code first, newer .net core, forgetting to do a migration or ModelBuilder misdirection.

The easiest fix is to make sure you have run the latest migration and updated the database, your model builder is correct. or that simply your data matches your types

Solution 2:[2]

You have a really strange code, in your action, it reminds me a data reader, looks like you are trying to add an item from IQueryable to IList.

Try this

List<Booking> list = _airPortContext.Booking.ToList();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TheGeneral
Solution 2 marc_s