title: ASP.Net MVC - Entity Framework 6 sometimes creates duplicate records?
body: <p>I have an ASP.Net MVC 5 application using EF6. I'm using Ninject for dependency injection. </p>
  
  <p>I have the following code to add comments to a ticket. But sometimes it creating duplicate record. Why is is this happening and how can I fix? </p>
  
  <p>So all in all I'm ending up with 3 comments being added as opposed to two with the user comments being duplicated - this is only happening occasionally.</p>
  
  <pre><code> var ticket = _unitOfWork.TicketRepository.GetTicket(dto.TicketId);
  
   using (var transaction = _unitOfWork.BeginTransaction())
   {
         try
         {
               // Add user posted comment
               var comment = new Comment
               {
                  TicketId = dto.TicketId,
                  Comments = dto.Comments
               };
  
               ticket.AddComment(comment);
  
               // save changes
               _unitOfWork.Complete();
  
               // Add a system comment
               ticket.AddComment(new Comment                    
               {
                  TicketId = dto.TicketId,
                  Comments = "&lt;b&gt;System Comment:&lt;/b&gt;&lt;br&gt;Some auto comment"
               });
  
               _notificationService.Send(ticket, comment, User.Identity.GetUserId());
  
               // save changes
               _unitOfWork.Complete();
  
               // Commit
               transaction.Commit();
  
               return Ok(Mapper.Map&lt;CommentDto&gt;(_unitOfWork.CommentRepository.GetComment(comment.Id)));
  
        }
        catch (Exception ex)
        {
              transaction.Rollback();
  
              return InternalServerError(ex);
         }
  
   }
  </code></pre>
  
  <p>These are the methods in my unitOfWork class:</p>
  
  <pre><code> public void Complete()
   {
          _context.SaveChanges();
   }
  
   public DbContextTransaction BeginTransaction()
   {
          return _context.Database.BeginTransaction();
   }
  </code></pre>
  
  <p>This is the add comment method in my Ticket class:</p>
  
  <pre><code> public void AddComment(Comment comment)
   {
          Comments.Add(comment);
   }
  </code></pre>
  
  <p><strong>* UPDATE *</strong></p>
  
  <p>The only other thing I'm aware of that uses db context is a background job to send email which runs every minute using hangfire but doesn't use unitofwork - see my code below:</p>
  
  <pre><code> [DisallowConcurrentExecution]
   public class EmailJob : IJob 
   {
  
      private readonly Logger _logger = LogManager.GetCurrentClassLogger();
      private readonly IUserNotificationRepository _userNotificationRepository;
      private readonly INotificationMailer _notificationMailer;
      private readonly ApplicationDbContext _context;
  
      public EmailJob()
      {
          _context = new ApplicationDbContext();
          _userNotificationRepository = new UserNotificationRepository(_context);
          _notificationMailer = new NotificationMailer();
      }
  
  
      public EmailJob(IUserNotificationRepository userNotificationRepository,
          INotificationMailer notificationMailer)
      {
          _userNotificationRepository = userNotificationRepository;
          _notificationMailer = notificationMailer;
      }
  
      // Use for hangfire
      public void Execute()
      {
          SendMail(_logger);
      }
  
      private void SendMail(Logger logger)
      {
          // some mail sending code...
  
          // Persist changes back to database
           _context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>Any ideas chaps?</p>