locked
EF and Concurrency Column RRS feed

  • Question

  • User264732274 posted

    code taken from http://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api.aspx

    see the code

    public class SchoolContext: DbContext 
        {
            public SchoolDBContext(): base() 
            {
            }
    
            public DbSet<Student> Students { get; set; }
            public DbSet<Standard> Standards { get; set; }
            
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //Set StudentName as concurrency column
                modelBuilder.Entity<Student>()
                        .Property(p => p.StudentName)
                        .IsConcurrencyToken();
            }
        }

    please tell me what is Concurrency Column and what it does and when to use it ?

    what is IsConcurrencyToken() ?

    thanks

    Thursday, September 15, 2016 12:35 PM

Answers

  • User765422875 posted

    It's important to understand Concurrency first. The following post goes over this very well:

    http://blog.sqlauthority.com/2012/11/15/sql-server-concurrency-basics-guest-post-by-vinod-kumar/

    • Concurrency can be defined as the ability of multiple processes to access or change shared data at the same time.
    • The greater the number of concurrent user processes that can be active without interfering with each other, the greater the concurrency of the database system.
    • Concurrency is reduced when a process that is changing data prevents other processes from reading that data or when a process that is reading data prevents other processes from changing that data.
    • Concurrency is also affected when multiple processes are attempting to change the same data simultaneously.
    • Two approaches to managing concurrent data access:
      • Optimistic Concurrency Model
      • Pessimistic Concurrency Model

    Entity Framework supports Optimistic Concurrency by default.

    http://www.entityframeworktutorial.net/EntityFramework5/handle-concurrency-in-entity-framework.aspx

    "EF saves the entity to the database, assuming that the same data has not changed since the entity was loaded. If it determines that the data has changed, then an exception is thrown and you must resolve the conflict before attempting to save it again."

    So answer your question more directly - the  Concurrency Column is a tracking column that can be used to determine when a row has been changed. As the link you provided says, StudenName will be included in the where clause in update and delete commands. And IsConcurrencyToken() configures the property to be used as an optimistic concurrency token.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 5:16 PM
  • User765422875 posted

    I'm not sure how I can better define this. It manages concurrency by including the concurrency column in the where clause for the SQL it builds when it needs to issue commands to the database (review post below).

    The following has examples of different ways of handling concurrency with EF. Your approach is the Fluent Api approach. You will see what I was implying above.

    http://www.codeproject.com/Articles/817432/Optimistic-Concurrency-in-Entity-Framework-Code-Fi

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 7:17 PM
  • User753101303 posted

    This is basically a db feature. You can use a rowversion column: https://msdn.microsoft.com/en-us/library/ms182776.aspx

    It is automatically updated with a new value each time the row is updated. When you tell EF to use this column as a concurrency column, it will use not only the pk but also the value for this column as part of the WHERE clause.

    So if someone else changed this row, this "version" value has been changed and the UPDATE statement won't find any more the row.

    If not using this special versioning column EF uses all non blob columns as part of the WHERE clause (so it is strongly suggested to use a version column which is used only for this purpose).

    This is not particular to EF. The same mechanism were used in ADO.NET or even ADO to handle optimistic concurrency.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 19, 2016 5:28 PM

All replies

  • User765422875 posted

    It's important to understand Concurrency first. The following post goes over this very well:

    http://blog.sqlauthority.com/2012/11/15/sql-server-concurrency-basics-guest-post-by-vinod-kumar/

    • Concurrency can be defined as the ability of multiple processes to access or change shared data at the same time.
    • The greater the number of concurrent user processes that can be active without interfering with each other, the greater the concurrency of the database system.
    • Concurrency is reduced when a process that is changing data prevents other processes from reading that data or when a process that is reading data prevents other processes from changing that data.
    • Concurrency is also affected when multiple processes are attempting to change the same data simultaneously.
    • Two approaches to managing concurrent data access:
      • Optimistic Concurrency Model
      • Pessimistic Concurrency Model

    Entity Framework supports Optimistic Concurrency by default.

    http://www.entityframeworktutorial.net/EntityFramework5/handle-concurrency-in-entity-framework.aspx

    "EF saves the entity to the database, assuming that the same data has not changed since the entity was loaded. If it determines that the data has changed, then an exception is thrown and you must resolve the conflict before attempting to save it again."

    So answer your question more directly - the  Concurrency Column is a tracking column that can be used to determine when a row has been changed. As the link you provided says, StudenName will be included in the where clause in update and delete commands. And IsConcurrencyToken() configures the property to be used as an optimistic concurrency token.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 5:16 PM
  • User264732274 posted
    Not very clear how EF manage concurrency. Better explain with example.
    Thursday, September 15, 2016 6:52 PM
  • User765422875 posted

    I'm not sure how I can better define this. It manages concurrency by including the concurrency column in the where clause for the SQL it builds when it needs to issue commands to the database (review post below).

    The following has examples of different ways of handling concurrency with EF. Your approach is the Fluent Api approach. You will see what I was implying above.

    http://www.codeproject.com/Articles/817432/Optimistic-Concurrency-in-Entity-Framework-Code-Fi

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 7:17 PM
  • User753101303 posted

    This is basically a db feature. You can use a rowversion column: https://msdn.microsoft.com/en-us/library/ms182776.aspx

    It is automatically updated with a new value each time the row is updated. When you tell EF to use this column as a concurrency column, it will use not only the pk but also the value for this column as part of the WHERE clause.

    So if someone else changed this row, this "version" value has been changed and the UPDATE statement won't find any more the row.

    If not using this special versioning column EF uses all non blob columns as part of the WHERE clause (so it is strongly suggested to use a version column which is used only for this purpose).

    This is not particular to EF. The same mechanism were used in ADO.NET or even ADO to handle optimistic concurrency.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 19, 2016 5:28 PM
  • User264732274 posted

    good link is http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

    Tuesday, September 20, 2016 12:01 PM