title: ASP.NET MVC SaveChangesAsync throwing DbUpdateException
body: <p>I'm new to Entityframework in my practice app I'm trying to initialize my newly created database with some sample data and using SaveChangesAsync() but its giving internal server error. When I migrated my DbContext there was no problem and the database created successfully.</p>
  
  <p>EDITED:
  after debugging I found <code>SqlException: Cannot insert explicit value for identity column in table 'Packages' when IDENTITY_INSERT is set to OFF</code> what should I do?</p>
  
  <h1>DbInitializer</h1>
  
  <pre><code>public class DbContextInitializer
  {
      private AppDbContext _context;
      private Data data = new Data();
  
      public DbContextInitializer(AppDbContext context)
      {
          _context = context;
      } 
  
      public async Task SeedData()
      {
          if (!_context.Packages.Any())
          {
              var pack1 = data.get(1);
              _context.Packages.Add(pack1);
  
              var pack2 = data.get(2);
              _context.Packages.Add(pack2);
  
              var pack3 = data.get(3);
              _context.Packages.Add(pack3);
  
              await _context.SaveChangesAsync();
          }
      }
  
  }
  </code></pre>
  
  <h1>DbContext</h1>
  
  <pre><code> public class AppDbContext : DbContext
    {
      public AppDbContext(DbContextOptions&lt;AppDbContext&gt; options) : base(options)
      {
  
      }
  
      public DbSet&lt;TableModel&gt; Packages { get; set; }
    }
  </code></pre>
  
  <h1>Configure in Startup.cs</h1>
  
  <pre><code>public void Configure(IApplicationBuilder app,
          IHostingEnvironment env,
          DbContextInitializer seed)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
              app.UseBrowserLink();
          }
          else
          {
              app.UseExceptionHandler("/Home/Error");
          }
  
          app.UseStaticFiles();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=App}/{action=Index}/{id?}");
          });
  
          seed.SeedData().Wait();
      }
  </code></pre>
  
  <h1>ConfigureServices in Startup.cs</h1>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
  
          services.AddDbContext&lt;AppDbContext&gt;(options =&gt; options.UseSqlServer(_configurationroot.GetConnectionString("ConString")));
          services.AddMvc();
          services.AddTransient&lt;ITableData, MockTableData&gt;();
          services.AddTransient&lt;DbContextInitializer&gt;();
      }
  </code></pre>
  
  <h1>Error</h1>
  
  <p><a href="https://i.stack.imgur.com/BgJVF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BgJVF.jpg" alt="Error"></a></p>