[英]Auto Increment Id MVC5
I am new to MVC and i'm trying to create an auto incrementing id upon using CRUD create. 我是MVC的新手,我正在尝试使用CRUD create创建一个自动递增的ID。 I've created my model with id : 我已创建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: 我从视图中删除了预订ID,但是当我尝试添加新预订时,出现以下异常:
'System.Data.Entity.Infrastructure.DbUpdateException' '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 System.Data.Entity.Internal.InternalContext.SaveChanges()\\ r \\ n位于System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\\ r \\ n位于System.Data.Entity.DbContext.SaveChanges() \\ r \\ n在c:\\ Users \\ User \\ Users \\ Documents \\ Visual Studio 2013 \\ Projects \\ CSCOpticians \\ CSCOpticians \\ Controllers \\ BookingsController.cs:第89行\\ r \\ n在clamb_method上的CSCOpticians.Controllers.BookingsController.Create(预订书) (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)\\ 在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object [] parameters)\\ r \\ n在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在System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()\\ r \\ n在System.Web.Mvc.Async.AsyncControllerActionInvoker .b__39(IAsyncResult asyncResult,ActionInvocation innerInvokeState)\\ r \\ n在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult)\\r\\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1。 System.Web.Mvc.Async.AsyncResultWrapper.End [)的End()\\ r \\ n.System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)的End [TResult](IAsyncResult asyncResult,Object tag)\\ r \\ n 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 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()处的r \\ n System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.r> n。<> c__DisplayClass46.b__3f()”字符串
您已在视图中删除BookingID还要切除BookingID在绑定
public ActionResult Create([Bind(Include = "Date,PracticeId,AppointmentTime,OpticianId,PatientId")] Booking booking)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.