MVC Entity Framework Database Record Update

I have a payment model with the boolean value "Status", which defaults to false. After the payment has been made, I need to update this specific payment β€œStatus” to β€œTrue”.

Here is the code that I tried to use to modify a specific record in the database, but that just doesn't change it. What am I doing wrong?

Payment payment = new Payment(); payment = db.Payments.Find(orderId); db.Entry(payment).State = EntityState.Modified; payment.Status = true; db.SaveChanges(); 

Thanks!

Here's what ended:

 using (var con = new ApplicationDbContext()) { payment = con.Payments.First(x => x.Id == orderId); payment.Status = true; con.Payments.Attach(payment); var entry = con.Entry(payment); entry.Property(e => e.Status).IsModified = true; con.SaveChanges(); } 
+5
source share
5 answers
 Payment payment = new Payment(); payment = db.Payments.Find(orderId); payment.Status = true; db.Entry(payment).State = EntityState.Modified; db.SaveChanges(); 
+6
source

The reason all these errors fail is because either the Payment object is never bound to a DBContext, or the orderId actually does not match the PK in the Payments table. In order for SaveChanges() to actually work, the object you are modifying must be tracked using DBContext, not just its EntityState set to Modified . Also, all of these examples seem overly complex.

 using (var db = new DbContext()) { // make sure you have the right column/variable used here var payment = db.Payments.FirstOrDefault(x => x.Id == orderId); if(payment == null) throw new Exception("Invalid id: " + orderId); // this variable is tracked by the db context payment.Status = true; db.SaveChanges(); } 
+3
source

Try the following:

  Payment payment; using (var context = new DBContext()) //replace the name of your context { payment = context.Payments.Find(orderId); } if(payment != null) { payment.Status = true; } using (var context = new DBContext()) //replace the name of your context { context.Payments.Attach(payment); context.Entry(payment).State = System.Data.EntityState.Modified; context.SaveChanges(); } 
+1
source

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.

+1
source

If you only have an object identifier, just do as Ali Golshhani said. But if you already have an object and you just want to update it, you can do this:

  public void Update(Payment payment, int orderId) { //first find the entity to update Payment oldEntity = DbSet.Find(orderId); //detach this entity from the DbSet Db.Entry(oldEntity).State = EntityState.Detached; //set the state from the entity that you just received to modified Db.Entry(obj).State = EntityState.Modified; } 

Disabling resolves the error message: "Attaching an object failed because another object of the same type already has the same primary key value."

I hope this helps.

0
source

Source: https://habr.com/ru/post/1202182/


All Articles