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, IDictionary
2 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.WrappedAsyncResultBase
1.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