简体   繁体   中英

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--------------

在此处输入图片说明

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

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 IQuerable to IList. Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM