Entity Framework Core in Action by Jon Smith

Entity Framework Core in Action by Jon Smith

Author:Jon Smith [Smith, Jon]
Language: eng
Format: epub, pdf
Tags: computers, Database Administration & Management, Programming, Object Oriented, Languages, .Net, Software Development & Engineering, General, internet, Web Programming, Web Services & APIs
ISBN: 9781617294563
Google: x519swEACAAJ
Publisher: Manning
Published: 2018-08-04T23:39:48.945520+00:00


Figure 8.7 After the user has changed the salary and clicked the Change button, the new salary and the original salary values are sent back to the web application. It then calls the UpdateSalary method, shown in listing 8.16, that both updates the salary and sets the original value expected in the database when it does the update. If a concurrency conflict is found, a new screen with an appropriate error message is shown to the user, who can then accept the existing database state, or apply their own update to the employee.

Listing 8.21 shows the entity class used for this example, with the Salary property set as a concurrency token. You also create a method called UpdateSalary that contains the code you need to execute in order to update the Salary property in such a way that DbUpdateConcurrencyException will be thrown if the Salary value has changed from the value originally shown on the user’s screen.

Listing 8.21 Entity class used to hold an employee’s salary with concurrency check

public class Employee { public int EmployeeId { get; set; } public string Name { get; set; } [ConcurrencyCheck] public int Salary { get; set; } ① public void UpdateSalary ② (DbContext context, int orgSalary, int newSalary) { Salary = newSalary; ③ context.Entry(this).Property(p => p.Salary) ④ .OriginalValue = orgSalary; ④ } }

① Salary property set as a concurrency token by the ConcurrencyCheck attribute.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.