title: Entity Framework Core Is Not Saving Changing to the Database After Setting the Entity State into Add
body: <p>In my case, I have a singleton service that depends on a scoped service, which is a DbContext implementation.</p>
  
  <p>The singleton service basically is a data access layer that performs the CRUD operation into the SQL server database.</p>
  
  <p>In the data access layer, I have injected the IServiceScopeFactory to get an instance of my DbContext per request. </p>
  
  <p>The following code block is showing a sample of the data access implementation:</p>
  
  <pre><code>public class Repository&lt;IEntity&gt; : IRepository&lt;IEntity&gt; where IEntity : BaseEntity
  {
      private readonly IServiceScopeFactory _scopeFactory;
  
      public Repository(
          IServiceScopeFactory scopeFactory)
      {
          _scopeFactory = scopeFactory;
      }
  
      public void Add(IEntity entity)
      {
          using (var scope = _scopeFactory.CreateScope())
          {
              var _context = scope.ServiceProvider.GetRequiredService&lt;PCPSContext&gt;();
              _context.Set&lt;IEntity&gt;().Add(entity);
              scope.Dispose();
          }
  
      }
  
      public Task&lt;int&gt; SaveChangesAsync()
      {
          Task&lt;int&gt; result;
          using (var scope = _scopeFactory.CreateScope())
          {
              var _context = scope.ServiceProvider.GetRequiredService&lt;PCPSContext&gt;();
              result = _context.SaveChangesAsync();
              scope.Dispose();
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>Registration of the data access service:</p>
  
  <pre><code>            services.AddSingleton(typeof(IRepository&lt;&gt;), typeof(Repository&lt;&gt;));
  </code></pre>
  
  <p>Registration of the dbContext:</p>
  
  <pre><code>var connection = configuration.GetConnectionString("PCPS_CS");
          LogManager.Configuration.Variables["connectionString"] = connection;
          services.AddDbContext&lt;PCPSContext&gt;(options =&gt;
          options.UseSqlServer(connection, b =&gt; b.MigrationsAssembly("PCPS.API")));
  </code></pre>
  
  <p>The problem is, the changes are not reflected in the SQL server database after saving the changes using EFcore, also there is no exception occurred during the process of adding an entity. </p>
  
  <p>I'm interested to know what causes the data to be not reflected in the database. </p>