1

I am new to MVC and i'm trying to create an auto incrementing id upon using CRUD create. I've created my model with id :

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int BookingId { get; set; }

and my controller:

 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);
    }

I have removed the Booking id from the view but when I try to add a new booking I am getting the following exception:

'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(Booking booking) in c:\Users\User\Documents\Visual Studio 2013\Projects\CSCOpticians\CSCOpticians\Controllers\BookingsController.cs:line 89\r\n at lambda_method(Closure , ControllerBase , Object[] )\r\n
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)\r\n at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters)\r\n at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters)\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()\r\n at 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.WrappedAsyncResultBase1.End()\r\n at 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 at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f()" string

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
coto2
  • 179
  • 4
  • 15
  • What is the message of the DbUpdateException? – Martin Noreke Aug 12 '15 at 15:50
  • @MartinNoreke I've added the stacktrace above – coto2 Aug 12 '15 at 15:57
  • The stack doesn't help, we need the message; it should tell you directly what the error is. – Brian Mains Aug 12 '15 at 16:07
  • @BrianMains The exception is at the db.SaveChanges(); - "An error occurred while updating the entries. See the inner exception for details" How do I view the inner exception? – coto2 Aug 12 '15 at 16:19
  • What happens when you replace `db.Bookings.Add(booking);` with (excuse formatting) `db.Bookings.Add(new Booking{Date = booking.Date, PracticeId = booking.PracticeId, AppointmentTime = booking.AppointmentTime, OpticianId = booking.OpticianId, PatientId = booking.PatientId});` – Tommy Aug 12 '15 at 18:49

1 Answers1

0

You have removed BookingID in the view but removed also the BookingID in Bind

public ActionResult Create([Bind(Include = "Date,PracticeId,AppointmentTime,OpticianId,PatientId")] Booking booking)
Nab
  • 64
  • 4