我是MVC的新手,我正在尝试使用CRUD创建一个自动递增ID。 我用id:
创建了我的模型 [Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BookingId { get; set; }
和我的控制员:
public ActionResult Create()
{
ViewBag.OpticianId = new SelectList(db.Opticans, "OpticianId", "UserId");
ViewBag.PatientId = new SelectList(db.Patients, "PatientId", "HCN");
ViewBag.PracticeId = new SelectList(db.Practices, "PracticeId", "PracticeName");
ViewBag.AppointmentTime = new SelectList(db.Times, "AppointmentTime", "AppointmentTime");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "BookingId,Date,PracticeId,AppointmentTime,OpticianId,PatientId")] Booking booking)
{
if (ModelState.IsValid)
{
db.Bookings.Add(booking);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.OpticianId = new SelectList(db.Opticans, "OpticianId", "UserId", booking.OpticianId);
ViewBag.PatientId = new SelectList(db.Patients, "PatientId", "HCN", booking.PatientId);
ViewBag.PracticeId = new SelectList(db.Practices, "PracticeId", "PracticeName", booking.PracticeId);
ViewBag.AppointmentTime = new SelectList(db.Times, "AppointmentTime", "AppointmentTime", booking.AppointmentTime);
return View(booking);
}
我已从视图中删除了预订ID,但当我尝试添加新预订时,我收到以下异常:
'System.Data.Entity.Infrastructure.DbUpdateException'
StackTrace“at System.Data.Entity.Internal.InternalContext.SaveChanges()\ r \ n at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\ r \ n at System.Data.Entity.DbContext.SaveChanges()\ r \ n at CSCOpticians.Controllers.BookingsController.Create(预订) c:\ Users \ User \ Documents \ Visual Studio 2013 \项目\ CSCOpticians \ CSCOpticians \ \控制器BookingsController.cs:行 89 \ r \ n在lambda_method(Closure,ControllerBase,Object [])\ r \ n
在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object []参数)\ r \ n at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2 parameters)\r\n at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 参数)\ r \ n at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod(个)\ r \ n 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult的 asyncResult,ActionInvocation innerInvokeState)\ r \ n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.End(个)\ r \ n 在System.Web.Mvc.Async.AsyncResultWrapper.End [TResult](IAsyncResult) asyncResult,Object tag)\ r \ n at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult的 asyncResult)\ r \ n at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d(个)\ r \ n 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()“string
答案 0 :(得分:0)
您已在视图中移除了 BookingID ,但也删除了 Bind
中的BookingIDpublic ActionResult Create([Bind(Include = "Date,PracticeId,AppointmentTime,OpticianId,PatientId")] Booking booking)