title: asp.net ef 7 Inserting when IDENTITY_INSERT is set to OFF problems
body: <p>When I try to save to my database I get the error </p>
  
  <blockquote>
    <p>SqlException: Cannot insert explicit value for identity column in table 'Photo' when IDENTITY_INSERT is set to OFF. </p>
  </blockquote>
  
  <p>I am using asp.net 5 MVC 6 with EF 7 on Visual Studio 2015
  There are a lot of similar questions. Most have solutions that are not supported by asp.net 5 MVC 6 or EF 7(One said to use a Data Annotation that would fix the problem in EF 6).  The others have not worked.  I try not to ask except as a last resort.</p>
  
  <p>My design is that every user will have many folders, and a folder will have many photos.</p>
  
  <p>I added <code>public ICollection&lt;UserFolder&gt; UserFolders { get; set; }</code> to ApplicationUser</p>
  
  <p>The Model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace FamPhotos.Models
  {
      public class UserFolder
      {
          public int ID { get; set; }
          public string Name { get; set; }
  
          public ICollection&lt;Photo&gt; Photo { get; set; }
  
          public string ApplicationUserId { get; set; }
          public virtual ApplicationUser ApplicationUser { get; set; }
      }
  
      public class Photo
      {
          public int ID { get; set; }
          public string Description { get; set; }
          public DateTime UploadDate { get; set; }
          public string Url { get; set; }
  
          public int UserFolderId { get; set; }
          public UserFolder UserFolder { get; set; }
  
      }
  }
  </code></pre>
  
  <p>The Controller method</p>
  
  <pre><code>// POST: Photos/Create
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; Create(Photo photo, IFormFile files, int id)
      {
          if (files == null)
          {
              ModelState.AddModelError(string.Empty, "Please select a file to upload.");
          }
          else if (ModelState.IsValid)
          {
              photo.UploadDate = DateTime.Now;
              photo.UserFolderId = id;
  
              var folderName = _context.UserFolder.Where(q =&gt; q.ID == id).Single().Name; 
  
              //TODO:  Check for image types
              var fileName = photo.ID.ToString() + ContentDispositionHeaderValue.Parse(files.ContentDisposition).FileName.Trim('"');
              var filePath = Path.Combine(_applicationEnvironment.ApplicationBasePath, "Photos", User.GetUserName(), folderName, fileName);
              await files.SaveAsAsync(filePath);
  
              photo.UserFolder = _context.UserFolder.Where(q =&gt; q.ID == id).Single();
              photo.Url = "~/Photos/" + fileName;
  
              _context.Add(photo);
              _context.SaveChanges();
  
  
              return RedirectToAction("Index");
          }
          return View(photo);
      }
  </code></pre>
  
  <p>The View:</p>
  
  <pre><code>    @model FamPhotos.Models.Photo
  
  @{
      ViewData["Title"] = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  &lt;form asp-action="Create" asp-controller="Photos" method="post" enctype="multipart/form-data"&gt;
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Photo&lt;/h4&gt;
          &lt;hr /&gt;
          &lt;div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"&gt;&lt;/div&gt;
          &lt;input type="file" name="files" /&gt;
          &lt;label asp-for="Description" class="col-md-2 control-label"&gt;&lt;/label&gt;
          &lt;div class="col-md-10"&gt;
              &lt;input asp-for="Description" class="form-control" /&gt;
              &lt;span asp-validation-for="Description" class="text-danger" /&gt;
          &lt;/div&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  
  &lt;div&gt;
      &lt;a asp-action="Index"&gt;Back to List&lt;/a&gt;
  &lt;/div&gt;
  
  @section Scripts {
      &lt;script src="~/lib/jquery/dist/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="~/lib/jquery-validation/dist/jquery.validate.min.js"&gt;&lt;/script&gt;
      &lt;script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"&gt;&lt;/script&gt;
  }
  </code></pre>
  
  <p>My DbContext:</p>
  
  <pre><code>   namespace FamPhotos.Models
  {
      public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          protected override void OnModelCreating(ModelBuilder builder)
          {
              builder.Entity&lt;UserFolder&gt;()
                  .HasMany(q =&gt; q.Photo)
                  .WithOne(c =&gt; c.UserFolder)
                  .HasForeignKey(c =&gt; c.UserFolderId);
  
  
  
              base.OnModelCreating(builder);
              // Customize the ASP.NET Identity model and override the defaults if needed.
              // For example, you can rename the ASP.NET Identity table names and more.
              // Add your customizations after calling base.OnModelCreating(builder);
          }
          public DbSet&lt;Photo&gt; Photo { get; set; }
          public DbSet&lt;ApplicationUser&gt; ApplicationUser { get; set; }
          public DbSet&lt;UserFolder&gt; UserFolder { get; set; }
      }
  }
  </code></pre>
  
  <p>Thanks.</p>