Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't SaveChanges with Entity Framework in ASP.Net MVC 3 project
    primarykey
    data
    text
    <p>Studying asp.net mvc 3 + EF code-first. I am new to both. My example is trivial, but I still can't make it work. Missing something simple and obvious...</p> <p>I've got a class:</p> <pre><code> public class Product { [HiddenInput(DisplayValue = false)] public int ProductID { get; set; } [Required(ErrorMessage = "Please enter a product name")] public string Name { get; set; } [Required(ErrorMessage = "Please enter a description")] [DataType(DataType.MultilineText)] public string Description { get; set; } [Required] [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")] public decimal Price { get; set; } [Required(ErrorMessage = "Please specify a category")] public string Category { get; set; } } </code></pre> <p>and a <code>DbContext</code>:</p> <pre><code>public class EFDbContext : DbContext { public DbSet&lt;Product&gt; Products { get; set; } } </code></pre> <p>and a repository:</p> <pre><code>public class EFProductRepository : IProductRepository { private EFDbContext context = new EFDbContext(); public IQueryable&lt;Product&gt; Products { get { return context.Products; } } public void SaveProduct(Product product) { if (product.ProductID == 0) context.Products.Add(product); context.SaveChanges(); } } </code></pre> <p>The mvc controller:</p> <pre><code>public class AdminController : Controller { private IProductRepository repository; public AdminController(IProductRepository repo) { repository = repo; } public ViewResult Index() { return View(repository.Products); } public ViewResult Edit(int productId) { Product product = repository.Products.FirstOrDefault(p =&gt; p.ProductID == productId); return View(product); } [HttpPost] public ActionResult Edit(Product product) { if (ModelState.IsValid) { repository.SaveProduct(product); TempData["message"] = string.Format("{0} has been saved", product.Name); return RedirectToAction("Index"); } else { // there is something wrong with the data values return View(product); } } } </code></pre> <p>It lets me see the list of products, opens the edit view, validates everything according to the set of attributes...</p> <p>When I save validated changes it goes to the Http Post <code>Edit</code> method and makes the necessary <code>SaveChanges()</code>.</p> <p>It doesn't throw any exceptions, it goes on and redirect me to the list of products.</p> <p>The edited item stays unchanged.</p> <p>The underlying database (connected through <code>connectionstrings</code> in <code>web.config</code>) stays unchanged as well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload