title: ASP.NET MVC not saving to the database?
body: <p>I'm trying to save my model to my database. I have my model set up and filled in the methods in the associated controller. When I run my web application and go through the create forms, it lets me click and does not give me any error, but it does not save anything to the database. Here is how my model looks.</p>
  <p>Assignment model:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Web;
  
  namespace ProjectVoting.Models
  {
      public class Assignment
      {
          [Key]
          public int Id { get; set; }
          public string StudentName { get; set; }
          public int Votes { get; set; }
      }
  }
  </code></pre>
  <p>This is what I am doing in my controller to create it.</p>
  <p>Assignment controller:</p>
  <pre><code>using ProjectVoting.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Web;
  using System.Web.Mvc;
  
  namespace ProjectVoting.Controllers
  {
      public class AssignmentController : Controller
      {
          ApplicationDbContext context;
  
          public AssignmentController()
          {
              context = new ApplicationDbContext();
          }
  
          // GET: Assignment
          public ActionResult Index()
          {
              return View(context.Assignments.ToList());
          }
  
  
          // GET: Assignment/Create
          [HttpGet]
          public ActionResult Create()
          {
              return View();
          }
  
          // POST: Assignment/Create
          [HttpPost]
          public ActionResult Create(Assignment assignment)
          {
              try
              {
                  if (ModelState.IsValid)
                  {
                      context.Assignments.Add(assignment);
                      context.SaveChanges();
                      return RedirectToAction(&quot;Index&quot;);
                  }
                  return View(assignment);
              }
              catch
              {
                  return View();
              }
          }
      }
  }
  </code></pre>
  <p>I'm trying to use the current local database that was automatically made when I created the project instead of making a separate database. Which is why I am using ApplicationDbContext and not a custom context which I created. In IdentityModels.cs I have added the following:</p>
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public ApplicationDbContext()
          : base(&quot;DefaultConnection&quot;, throwIfV1Schema: false)
      {
      }
  
      public DbSet&lt;Assignment&gt; Assignments { get; set; }
  
      public static ApplicationDbContext Create()
      {
          return new ApplicationDbContext();
      }
  }
  </code></pre>
  <p>In order to make sure my model is associated with the current context.</p>
  <p>My server explorer looks like this:</p>
  <p><a href="https://i.stack.imgur.com/LQzPE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LQzPE.png" alt="Enter image description here" /></a></p>
  <p>I have used breakpoints and ModelState is valid, I enter that block. However when I get to context.SaveChanges(); it skips to the catch block which means something is happening in that statement. I added a catch (Exception e) into that statement, and when I hover over it while running it with break points, it reads &quot;An error occurred while updating the entries. See the inner exception for details.&quot;. When I inspect it, it says &quot;Cannot insert the value NULL into column 'Id', table 'aspnet-ProjectVoting-20161203051219.dbo.Assignments'&quot;</p>
  <p>Why is it failing to save to the database?</p>
  <hr/>
  <h3>Resolution</h3>
  <p>It has been fixed. The database needs to be created with the following attributes:</p>
  <pre><code>CREATE TABLE [dbo].[Assignments] (
      [Id]          INT          IDENTITY (1, 1) NOT NULL,
      [StudentName] VARCHAR (50) NULL,
      [Votes]       INT          NULL,
      PRIMARY KEY CLUSTERED ([Id] ASC)
  );
  </code></pre>
  <p>My ID was not auto incrementing, and it was trying to set it to null when posting.</p>