RazorPages Crud IIS Application giving DbUpdateConcurrencyException

I have been learning Asp.Net frameworks and while following Razor Pages Tutorial i’m getting DbUpdateConcurrencyException exception on my IIS deployed application. I have tried using Exception Handling as suggested by the tutorial but i still get same exception. So what i’m doing here is while creating an object i get DbUpdateConcurrencyException but on Edit and Delete there’s no exception. So i can’t create any objects from CRUD app but can add from SQL Server Management Tool. Can someone help recognise where i’m going wrong?

As Suggested in exception traceback, it says Create.Cshtml.cs

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using RazorPagesMovie.Data; using RazorPagesMovie.model;  namespace RazorPagesMovie.Pages.Movies {     public class CreateModel : PageModel     {         private readonly RazorPagesMovie.Data.RazorPagesMovieContext _context;          public CreateModel(RazorPagesMovie.Data.RazorPagesMovieContext context)         {             _context = context;         }          public IActionResult OnGet()         {             return Page();         }          [BindProperty]         public Movie Movie { get; set; }          public async Task<IActionResult> OnPostAsync()         {             if (!ModelState.IsValid)             {                 return Page();             }               _context.Movie.Add(Movie);              await _context.SaveChangesAsync();              return RedirectToPage("./Index");         }     } } 

model/Movie.cs

using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks;  namespace RazorPagesMovie.model {     public class Movie     {         public int ID { get; set; }         public string Title { get; set; }          [DataType(DataType.Date)]         public DateTime ReleaseDate { get; set; }         public string Genre { get; set; }         public decimal Price { get; set; }     }  } 

Create.cshtml

@page @model RazorPagesMovie.Pages.Movies.CreateModel  @{     ViewData["Title"] = "Create"; }  <h1>Create</h1>  <h4>Movie</h4> <hr /> <div class="row">     <div class="container">         <form method="post">             <div asp-validation-summary="ModelOnly" class="text-danger"></div>             <div class="form-group">                 <label asp-for="Movie.Title" class="control-label"></label>                 <input asp-for="Movie.Title" class="form-control" />                 <span asp-validation-for="Movie.Title" class="text-danger"></span>             </div>             <div class="form-group">                 <label asp-for="Movie.ReleaseDate" class="control-label"></label>                 <input asp-for="Movie.ReleaseDate" class="form-control" />                 <span asp-validation-for="Movie.ReleaseDate" class="text-danger"></span>             </div>             <div class="form-group">                 <label asp-for="Movie.Genre" class="control-label"></label>                 <input asp-for="Movie.Genre" class="form-control" />                 <span asp-validation-for="Movie.Genre" class="text-danger"></span>             </div>             <div class="form-group">                 <label asp-for="Movie.Price" class="control-label"></label>                 <input asp-for="Movie.Price" class="form-control" />                 <span asp-validation-for="Movie.Price" class="text-danger"></span>             </div>             <div class="form-group">                 <input type="submit" value="Create" class="btn btn-primary" />             </div>         </form>     </div> </div>  <div>     <a asp-page="Index">Back to List</a> </div>  @section Scripts {     @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} } 

Here the error page

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.