title: DbContext not saving
body: <p>It's been awhile since I have been playing with <strong>DbContext</strong>. I have been using a Serivce / Repository / UnitOfWork pattern for a number of years now, which has enabled me to use entity framework without worrying about how to add / update and delete.</p>
  
  <p>Now I have decided that for some projects using that design pattern is not suitable (mainly when working with lookup tables). Because of this I have gone back to basics.</p>
  
  <p>Using the <strong>DbContext</strong> has been find for getting information, but now I have actually started creating my methods for inserting / updating data I seem to have found an issue.
  I am fairly certain that the issue is something simple, but I need some help :)</p>
  
  <p>Here is my method:</p>
  
  <pre><code>[HttpPost]
  [Route("")]
  public IHttpActionResult Import(IList&lt;CollectionBindingModel&gt; models)
  {
  
      // If our ModelState is invalid, return a bad request
      if (!ModelState.IsValid)
          return BadRequest(ModelState);
  
      try
      {
          // Loop through our models
          foreach (var model in models)
          {
  
              // Create our collection
              var collection = new Collection()
              {
                  CenterId = model.CenterId,
                  Reference = model.Reference,
                  CustomerReference = model.CustomerReference,
                  CustomerName = model.CustomerName,
                  CustomerBusinessName = model.CustomerBusinessName,
                  SupplierName = model.SupplierName,
                  CollectionCode = model.CollectionCode,
                  Status = model.Status,
                  CollectionDate = model.CollectionDate
              };
  
              // Add to our database context
              this.DbContext.Collections.Add(collection);
          }
  
          // Save our changes
          this.DbContext.SaveChanges();
      }
          catch (Exception ex)
      {
  
      }
  
      // Return Ok
      return Ok();
  }
  </code></pre>
  
  <p>I have merely added the try / catch block for testing purposes.
  When I call this method from my interface, it returns OK and throws no errors, but if I check the database I see there have been no inserts.</p>
  
  <p>Can anyone tell me why?</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>So, digging a little deeper. I have a base controller which looks like this:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Handles the creation of universal properties and methods
  /// &lt;/summary&gt;
  public class BaseController : ApiController
  {
  
      // Create our public properties
      protected DatabaseContext DbContext { get { return new DatabaseContext(); } }
      protected UserService UserService { get { return Request.GetOwinContext().GetUserManager&lt;UserService&gt;(); } }
      protected RoleService RoleService { get { return Request.GetOwinContext().GetUserManager&lt;RoleService&gt;(); } }
      protected ModelFactory ModelFactory { get { return new ModelFactory(this.Request, this.UserService); } }
  
      /// &lt;summary&gt;
      /// Used to return the correct error from an Identity Result
      /// &lt;/summary&gt;
      /// &lt;param name="result"&gt;The Identity Result to process&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      protected IHttpActionResult GetErrorResult(IdentityResult result)
      {
  
          // If there is no result, return an internal server error
          if (result == null)
              return InternalServerError();
  
          // If we have an error
          if (!result.Succeeded)
          {
  
              // If we have some errors
              if (result.Errors != null)
              {
  
                  // For each error, add to the ModelState
                  foreach (var error in result.Errors)
                      ModelState.AddModelError("", error);
              }
  
              // If our ModelState is valid
              if (ModelState.IsValid)
              {
  
                  // No ModelState errors are available to send, so just return an empty BadRequest.
                  return BadRequest();
              }
  
              // Return a BadRequest with our ModelState
              return BadRequest(ModelState);
          }
  
          // Return null if no errors are found
          return null;
      }
  }
  </code></pre>
  
  <p>Which is where the <strong>DbContext</strong> is pulled from.
  If I change my controller method to this:</p>
  
  <pre><code>[HttpPost]
  [Route("")]
  public IHttpActionResult Import(IList&lt;CollectionBindingModel&gt; models)
  {
  
      // If our ModelState is invalid, return a bad request
      if (!ModelState.IsValid)
          return BadRequest(ModelState);
  
      try
      {
          using (var context = new DatabaseContext())
          {
  
              // Loop through our models
              foreach (var model in models)
              {
  
                  // Create our collection
                  var collection = new Collection()
                  {
                      CenterId = model.CenterId,
                      Reference = model.Reference,
                      CustomerReference = model.CustomerReference,
                      CustomerName = model.CustomerName,
                      CustomerBusinessName = model.CustomerBusinessName,
                      SupplierName = model.SupplierName,
                      CollectionCode = model.CollectionCode,
                      Status = model.Status,
                      CollectionDate = model.CollectionDate
                  };
  
                  // Add to our database context
                  context.Collections.Add(collection);
              }
  
              // Save our changes
              context.SaveChanges();
          }
      }
      catch (Exception ex)
      {
          return InternalServerError(ex);
      }
  
      // Return Ok
      return Ok();
  }
  </code></pre>
  
  <p>then everything saves OK.</p>