title: EF Core insert explicit value for identity column
body: <p>I have problem with inserting data to database table which has foreign keys. 
  As error I'm getting </p>
  
  <blockquote>
    <p>Cannot insert explicit value for identity column in table
    'MachineTypes' when IDENTITY_INSERT is set to OFF.Cannot insert
    explicit value for identity column in table 'SpareTypes' when
    IDENTITY_INSERT is set to OFF.</p>
  </blockquote>
  
  <p>BaseEntity.cs</p>
  
  <pre><code>public abstract class BaseEntity
  {
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public Int64 Id { get; set; }
  
      public DateTime CreateDate { get; set; }
  }
  </code></pre>
  
  <p>MachineType.cs</p>
  
  <pre><code>public class MachineType : BaseEntity
  {
      [Required]
      [StringLength(50)]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>SpareType.cs</p>
  
  <pre><code>   public class SpareType : BaseEntity
      {
          [Required]
          [StringLength(25)]
          public string Name { get; set; }
      }
  </code></pre>
  
  <p>SparePart.cs</p>
  
  <pre><code>public class SparePart : BaseEntity
  {
  [Required]
  [StringLength(100)]
  public string InternalCode { get; set; }
  
  [StringLength(4096)]
  public string Description { get; set; }
  
  [StringLength(255)]
  public string NameOnFolder { get; set; }
  
  public decimal? Enter { get; set; }
  
  public decimal? Exit { get; set; }
  
  public decimal? Thickness { get; set; }
  
  public string Band { get; set; }
  
  public string Color { get; set; }
  
  public bool Elastic { get; set; }
  
  [Required]
  public virtual MachineType MachineType { get; set; }
  
  [Required]
  public virtual SpareType SpareType { get; set; }
  }
  </code></pre>
  
  <p>SparePartViewModel.cs</p>
  
  <pre><code> public class SparePartViewModel
      {
          public int Id { get; set; }
  
          public DateTime CreateDate { get; set; }
  
          [Required]
          [StringLength(100)]
          public string InternalCode { get; set; }
  
          [StringLength(4096)]
          public string Description { get; set; }
  
          [StringLength(255)]
          public string NameOnFolder { get; set; }
  
          public decimal? Enter { get; set; }
  
          public decimal? Exit { get; set; }
  
          public decimal? Thickness { get; set; }
  
          public string Band { get; set; }
  
          public string Color { get; set; }
  
          public bool Elastic { get; set; }
  
  
  
          [Required]
          public virtual MachineType MachineType { get; set; }
  
          [Required]
          public virtual SpareType SpareType { get; set; }
      }
  </code></pre>
  
  <p>Controller for posting</p>
  
  <pre><code>[Route("/api/v1/items")]
  public class SparePartController : Controller
  {
  private IRepository&lt;SparePart&gt; _repoSparePart;
  
  
  public SparePartController(IRepository&lt;SparePart&gt; repoSparePart)
  {
      _repoSparePart = repoSparePart;
  
  }
  
  [HttpPost("")]
  public async Task&lt;IActionResult&gt; Post([FromBody]SparePartViewModel viewModel)
  {
      if (ModelState.IsValid)
      {
          var newItem = Mapper.Map&lt;SparePart&gt;(viewModel);
          newItem.CreateDate = DateTime.Now;
  
          _repoSparePart.Insert(newItem);
  
          if (await _repoSparePart.SaveChangesAsync())
          {
              return Created($"items/{newItem.InternalCode}", Mapper.Map&lt;SparePartViewModel&gt;(viewModel));
          }
      }
      return BadRequest("Failed to save.");
  }
  </code></pre>
  
  <p>}</p>
  
  <p>AppContext.cs</p>
  
  <pre><code>   public class AppContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          private IConfigurationRoot _config;
  
          public AppContext(IConfigurationRoot config, DbContextOptions options) : base(options)
          {
              _config = config;
          }
  
          public DbSet&lt;SpareType&gt; SpareTypes { get; set; }
          public DbSet&lt;MachineType&gt; MachineTypes { get; set; }
          public DbSet&lt;SparePart&gt; SpareParts { get; set; }
  
          protected override void OnModelCreating(ModelBuilder builder)
          {
              builder.Entity&lt;SpareType&gt;()
                  .HasIndex(s =&gt; new { s.Name })
                  .IsUnique(true);
  
              builder.Entity&lt;MachineType&gt;()
                  .HasIndex(s =&gt; s.Name)
                  .IsUnique(true);
  
  
              base.OnModelCreating(builder);
          }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              base.OnConfiguring(optionsBuilder);
              optionsBuilder.UseSqlServer(_config["ConnectionStrings:RndDbContextConnection"]);
          }
      }
  </code></pre>
  
  <p>During the posting all the data are catch correctly, but when should be inserted into database I'm getting an error.
  <a href="https://i.stack.imgur.com/lBFWu.png" rel="noreferrer"><img src="https://i.stack.imgur.com/lBFWu.png" alt="enter image description here"></a></p>