title: Microsoft Entity Framework is not saving changes
body: <p>Im currently trying to implement CRUD functionality with a dbfactory and generics with microsoft EF, but while listing entries is working, making changes to the db is currently not working.</p>
  <pre><code> public class AbstractDataModel
      {
          [Key]
          public Guid gid { get; set; }
      }
  </code></pre>
  <p>Model</p>
  <pre><code>class SalesOrder : AbstractDataModel
  {
      public int salesOrderID { get; set; }
      public int productID { get; set; }
      public int customerID { get; set; }
      public Guid createdBy { get; set; }
      public string dateCreated { get; set; }
      public string orderDate { get; set; }
      public string orderStatus { get; set; }
      public string dateModified { get; set; }
  }
  </code></pre>
  <p>A DBCore with some other functionality besides the ones listed here, which are not relevant for the factory</p>
  <pre><code> public class DBCore : DbContext
      {
      public static string connectionString = &quot;myConnectionStringToDb&quot;;
  
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseSqlServer(connectionString);
      }
  </code></pre>
  <p>Data Service which calls factory</p>
  <pre><code>class SalesOrderService : DBCore
  {
      public DbSet&lt;SalesOrder&gt; SalesOrders { get; set; }
  
      public OkObjectResult GetAllSalesOrders()
      {
          DBFactory factory = new DBFactory();
          return new OkObjectResult(JsonConvert.SerializeObject(factory.GetAll(SalesOrders)));
      }
  
      public OkObjectResult AddSalesOrder(SalesOrder order)
      {
          order.gid = Guid.NewGuid();
          return DBFactory.AddOne(order);
      }
      public OkObjectResult UpdateSalesOrder(SalesOrder order)
      {
          return DBFactory.UpdateOne(order);
      }
      public OkObjectResult DeleteSalesOrder(SalesOrder order)
      {
          return DBFactory.DeleteOne(order);
      }    
  }
  </code></pre>
  <p>simple CRUD-Factory,</p>
  <pre><code>       class DBFactory : DBCore
  {
      public DbSet&lt;UserModel&gt; Users { get; set; }
      public DbSet&lt;SalesOrder&gt; SalesOrders { get; set; }
  
      public List&lt;T&gt; GetAll&lt;T&gt;(DbSet&lt;T&gt; dbset) where T : class
      {
          using (this)
          {
              return dbset.ToList();
          }
      }
      public static OkObjectResult AddOne&lt;T&gt;(T data)
      {
          using (DBFactory factory = new DBFactory())
          {
              factory.Add(data);
              factory.SaveChanges();
              return new OkObjectResult(&quot;Entry was sucessfully added&quot;);
          }
      }
      public static OkObjectResult UpdateOne&lt;T&gt;(T data) 
      { 
          using (DBFactory factory = new DBFactory())
          {
              factory.Update(data);
              factory.SaveChanges();
              return new OkObjectResult(&quot;Entry was sucessfully updated&quot;);
          }
      }
      public static OkObjectResult DeleteOne&lt;T&gt;(T data)
      {
          using (DBFactory factory = new DBFactory())
          {
              factory.Attach(data);
              factory.Remove(data);
              factory.SaveChanges();
              return new OkObjectResult(&quot;Entry was sucessfully removed&quot;);
          }
      }
  }
  </code></pre>
  <p>Edit: Following the advices i changed the code so it should SaveChanges for the Factory, which also contains the context as a property. But it still doesnt seem to work for all database operations except listing all entries</p>
  <p>Editv2: Thanks for the adivces it seems i have solved that problem, but a new one appeared :D
  I can now do database operations like deleting entries, but now i cant list the entries anymore because the following error occurs, although the code there didnt really change:</p>
  <p><em>&quot;Executed 'GetAllOrders' (Failed, Id=5fb95793-572a-4545-ac15-76dffaa7a0cf, Duration=74ms)
  [2020-10-23T14:33:43.711] System.Private.CoreLib: Exception while executing function: GetAllOrders. Newtonsoft.Json: Self referencing loop detected for property 'Context' with type 'FicoTestApp.Models.SalesOrder'. Path '[0].ChangeTracker'.&quot;</em></p>