Click here to Skip to main content
15,112,866 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am getting error like this

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.


here is my code StoreMangerController.

public ActionResult Edit(int id)
        {
            Album album = db.Albums.Find(id);
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        }


SQL
[HttpPost]
       public ActionResult Edit(Album album)
       {
           if (ModelState.IsValid)
           {
               db.Entry(album).State = EntityState.Modified;
               db.SaveChanges();
               return RedirectToAction("Index");
           }
           ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
           ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
           return View(album);
       }


in Model
Album.cs
SQL
namespace MusicStore.Models
{
    [Bind(Exclude="AlbumId")]
    public class Album
    {
        [ScaffoldColumn(false)]
        public int AlbumId { get; set; }

        [DisplayName("Genre")]
        public int GenreId { get; set; }

        [DisplayName("Artist")]
        public int ArtistId { get; set; }

        [Required(ErrorMessage="An Album title is required")]
        [StringLength(160)]
        public string Title { get; set; }

        [Required(ErrorMessage = "Price is required")]
        [Range(0.01,100.00,ErrorMessage="Price must be between 0.01 and 100.00")]
        public decimal Price { get; set; }
        public string AlbumArtUrl { get; set; }
        public Artist Artist { get; set; }

        public Genre Genre { get; set; }
    }
}


please help to solve the problem.

thanks and regards
sreekanth
Posted

1 solution

You are not clear, but I think this is the issue:

C#
db.Entry(album).State = EntityState.Modified;
              db.SaveChanges();



You don't do this. You change the object itself, from the DB, the objects sent to your view won't do that. Then the system works out what is modified and saves it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900