自动增量Id MVC5



我是新的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 " atSystem.Data.Entity.Internal.InternalContext.SaveChanges () r nSystem.Data.Entity.Internal.LazyInternalContext.SaveChanges () r nSystem.Data.Entity.DbContext.SaveChanges () r nCSCOpticians.Controllers.BookingsController。创建(预订预订)c:UsersUserDocumentsVisual工作室2013 CSCOpticians CSCOpticians 控制器项目 BookingsController.cs:行89rn at lambda_method(Closure, ControllerBase, Object[])rn
System.Web.Mvc.ActionMethodDispatcher.Execute (ControllerBase控制器,对象[]参数)rn atSystem.Web.Mvc.ReflectedActionDescriptor.Execute (ControllerContext控制器上下文,字典2 parameters)rn at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary参数) r nSystem.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod () r n在System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39 (IAsyncResultasyncResult, ActionInvocation innerInvokeState)rn atSystem.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult)rn at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase . end ()rn在System.Web.Mvc.Async.AsyncResultWrapper.End TResult (IAsyncResultasyncResult,对象标签)rn atSystem.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod (IAsyncResultasyncResult) r nSystem.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d () r n在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters灵活;> c__DisplayClass46.b__3f()"字符串

您已经在视图中删除了BookingID,但也删除了Bind

中的BookingID
public ActionResult Create([Bind(Include = "Date,PracticeId,AppointmentTime,OpticianId,PatientId")] Booking booking)

最新更新