none
How to handle concurrency with EF code first RRS feed

  • Question

  • how to avoid to work with same data by two people. suppose a console apps using EF to perform CRUD operation and that console apps is running in two pc. so if two apps try to work with same data or try to change same data then how EF can handle this situation.

    please guide me in details. thanks

    Friday, September 16, 2016 2:27 PM

Answers

  • Hi Mou_inn,

    You could use Data Annotation or Fluent API for handling optimistic concurrency.

    1. Data Annotation, Code First uses [Timestamp] attribute for handling optimistic concurrency. like this:

    using System.ComponentModel.DataAnnotations;
    
    namespace ConcurrencyCheck.Models
    {
        public class Student
        {
            public int StudentId { get; set; }
    
            public string RollNumber { get; set; }
    
            public string FirstName { get; set; }
    
            public string LastName { get; set; }
    
            [Timestamp]
            public byte[] RowVersion { get; set; }
        }
    }

    2. Fluent API, The fluent API uses IsRowVersion() method for configuring optimistic concurrency.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>().Property(s => s.RowVersion).IsRowVersion();
        base.OnModelCreating(modelBuilder);
    }

    For more information, please refer to:

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

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Wednesday, September 21, 2016 8:08 AM
    Monday, September 19, 2016 2:56 AM
    Moderator

All replies

  • how to avoid to work with same data by two people. suppose a console apps using EF to perform CRUD operation and that console apps is running in two pc. so if two apps try to work with same data or try to change same data then how EF can handle this situation.

    please guide me in details. thanks


    I gave you a tried and true method on how to handle it, which I learned back in the 1980's on the big IBM iron horse and using COBOL that I carried across to MS and client/server.
    Friday, September 16, 2016 3:33 PM
  • Hi Mou_inn,

    You could use Data Annotation or Fluent API for handling optimistic concurrency.

    1. Data Annotation, Code First uses [Timestamp] attribute for handling optimistic concurrency. like this:

    using System.ComponentModel.DataAnnotations;
    
    namespace ConcurrencyCheck.Models
    {
        public class Student
        {
            public int StudentId { get; set; }
    
            public string RollNumber { get; set; }
    
            public string FirstName { get; set; }
    
            public string LastName { get; set; }
    
            [Timestamp]
            public byte[] RowVersion { get; set; }
        }
    }

    2. Fluent API, The fluent API uses IsRowVersion() method for configuring optimistic concurrency.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>().Property(s => s.RowVersion).IsRowVersion();
        base.OnModelCreating(modelBuilder);
    }

    For more information, please refer to:

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

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Wednesday, September 21, 2016 8:08 AM
    Monday, September 19, 2016 2:56 AM
    Moderator