As mentioned here :
The Find method in DbSet uses the primary key value to try to find the object tracked by the context. If the object is not found in the context, then the request will be sent to the database to find the object there. Null is returned if the object is not found in the context or in the database.
Therefore, make sure that the payment class is as follows:
public class Payment { [Key] public int Id {get; set;} public bool Status {get; set;} }
And your input save logic might look like this:
Payment payment = null; using (var ts = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(1, 0, 0, 0))) { using (var context = new DBContext()) { context.Database.Log = s => { System.Diagnostics.Debug.WriteLine(s); }; payment = context.Payments.Find(orderId); if(payment != null) { payment.Status = true; context.Entry(payment).State = System.Data.EntityState.Modified; } else { context.Payments.Add(new Payment(){ Status = true }); } context.SaveChanges(); } ts.Complete(); }
A transaction pane has been added to ensure that it is open and closed correctly and SQL query registration has been added to debug the window.
aleha source share