chapter ten

10 Configuring advanced features and handling concurrency conflicts

 

This chapter covers

  • Using an SQL user-defined function in EF Core queries
  • Configuring columns to have default values or computed values
  • Configuring SQL column properties on databases not created by EF Core
  • Handling concurrency conflicts

This chapter starts several advanced configuration features that directly interact with your SQL database, such as using SQL user-defined functions (UDFs), computed columns and so on. These features allow you to move some of your calculations or settings into the SQL database. Although you won’t use these features every day, they can be useful in specific circumstances.

The second half of this chapter is about handling multiple, near-simultaneous updates of the same piece of data in the database; these updates can cause problems known as concurrency conflicts. You’ll learn how to configure just one property/column or a whole entity/table to catch concurrency conflicts, and how to capture and then write code to correct the concurrency conflict.

10.1  DbFunction—using user-defined functions with EF Core

10.1.1    Configuring a scalar-valued user-defined function

10.1.2    Configuring a table-valued user-defined function

10.1.3    Adding your UDF code to the database

10.1.4    Using a registered UDF in your database queries

10.2  Computed column—a dynamically calculated column value

10.3  Setting a default value for a database column

10.3.1    Configuring a property to have a SQL DEFAULT column in the database

10.3.2    Creating a value generator to generate a default value dynamically

10.4  Sequences—providing numbers in a strict order

10.5  Marking database-generated properties

10.5.1    Marking a column that’s generated on an addition or update

10.5.2    Marking a column’s value as set on insert of a new row

10.5.3    Marking a column/property as “normal”

10.6  Handling simultaneous updates—concurrency conflicts

10.6.1    Why do concurrency conflicts matter?

10.6.2    EF Core’s concurrency conflict–handling features

Detecting a concurrent change via concurrency token

Detecting a concurrent change via timestamp

10.6.3    Handling a DbUpdateConcurrencyException

10.6.4    The disconnected concurrent update issue

10.7  Summary