【发布时间】:2019-10-29 05:48:21
【问题描述】:
谁能帮我解决这个演员表问题?这是我的代码。当我在 using(TaxiPlannerEntities) 开始时使用断点时,它不会进入 foreach,而是直接进入 catch 异常 e 并向我显示此错误。
public class BookingController : Controller
{
/*[HttpPost]*/
// [Route("InsertBooking")]
public string Book(int employee_ID , List<DateTime> datebooked)
{
using (TaxiPlannerEntities contextx = new TaxiPlannerEntities())
{
try
{
//Create new booking
var booking1 = new booking();
booking1.uid = employee_ID;
List<DateTime> y = new List<DateTime>();
y.Add(Convert.ToDateTime(datebooked));
/* y.Add(DateTime.Now);
y.Add(DateTime.Now.AddDays(1));
y.Add(DateTime.Now.AddDays(2));
y.Add(DateTime.Now.AddDays(3));*/
/* y.Add(Convert.ToDateTime(datebooked));*/
foreach (DateTime x in y)
{
var db1 = new booking_date
{
bookingDate = x
};
db1.status = "pending";
booking1.booking_date.Add(db1);
}
contextx.bookings.Add(booking1);
contextx.SaveChanges();
return "success";
}
catch (Exception e)
{
// System.Diagnostics.Debug.WriteLine("test");
return e.Message;
}
}
}
}
}
【问题讨论】:
-
使用 Console.WriteLine(e.StackTrace);在 catch 子句中查找导致异常的行。
-
您正尝试将整个
List<DateTime>转换为Convert.ToDateTime(datebooked),将转换结果添加单个元素到另一个List<DateTime>。这种转换应该达到什么目的? -
@Arphile 我已经尝试过了,但它没有显示该行导致异常
-
y中有没有数据。 y 中的数据计数是多少?
标签: c# api entity-framework-6