尝试检索数据时出现 invalidcastexception
Getting an invalidcastexception when trying to retrieve data
当 运行 get 方法到 return 填充了详细信息的视图时,我得到异常说:
System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.DateTime'.'
代码代码是这样的:
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);
}
我不确定为什么要尝试将对象转换为日期类型
我的模型是这样的:
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; }
}
----------------更新----------------
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");
});
------------更新-------------
你的代码真的很奇怪,在你的操作中,它让我想起了一个数据 reader,看起来你正试图添加一个从 IQueryable
到 IList
的项目。
试试这个
List<Booking> list = _airPortContext.Booking.ToList();
这个错误可能是由于数据库中的类型与模型的类型不匹配造成的。
注意:这可能有很多原因,具体取决于这是代码优先、较新的 .net 核心、忘记执行迁移还是 ModelBuilder 误导。
最简单的解决方法是确保您有 运行 最新的迁移并更新了数据库,您的模型构建器是正确的。或者只是您的数据与您的类型匹配
当 运行 get 方法到 return 填充了详细信息的视图时,我得到异常说:
System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.DateTime'.'
代码代码是这样的:
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);
}
我不确定为什么要尝试将对象转换为日期类型
我的模型是这样的:
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; }
}
----------------更新----------------
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");
});
------------更新-------------
你的代码真的很奇怪,在你的操作中,它让我想起了一个数据 reader,看起来你正试图添加一个从 IQueryable
到 IList
的项目。
试试这个
List<Booking> list = _airPortContext.Booking.ToList();
这个错误可能是由于数据库中的类型与模型的类型不匹配造成的。
注意:这可能有很多原因,具体取决于这是代码优先、较新的 .net 核心、忘记执行迁移还是 ModelBuilder 误导。
最简单的解决方法是确保您有 运行 最新的迁移并更新了数据库,您的模型构建器是正确的。或者只是您的数据与您的类型匹配