How to set Optimistic Concurrency, Entity Framework 6

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
[email protected]
Posts: 2
Joined: Fri 25 Jan 2019 03:15

How to set Optimistic Concurrency, Entity Framework 6

Post by [email protected] » Fri 25 Jan 2019 07:22

I'm trying to build Optimistic Concurrency control with .Net 4.7 + Entity Framework 6(CodeFirst) + PostgreSQL10.

I have read these articles.
With Npgsql ,NpgsqlUseXminAsConcurrencyToken is available.
However I haven't found how to with dotConnect.
Could you tell me How can I do with dotConnect?

Thank you for reading.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: How to set Optimistic Concurrency, Entity Framework 6

Post by Shalex » Tue 29 Jan 2019 15:19

We will investigate the question and notify you about the result.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: How to set Optimistic Concurrency, Entity Framework 6

Post by Shalex » Tue 05 Feb 2019 19:05

Here is an example of setting Optimistic Concurrency with Entity Framework 6 via dotConnect for PostgreSQL.

Class

Code: Select all

  public class MyClass {

    public int Id { get; set; }

    [MaxLength(100)]
    public string FStringField { get; set; }

    [ConcurrencyCheck]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string xmin { get; set; }
  }
Contexts (MyContextWithoutXmin is required only for the Database.Create() functionality)

Code: Select all

  public class MyContext: DbContext {

    public MyContext()
      : base(new PgSqlConnection("User Id=postgres;Password=postgres;Host=db;Port=5432;Database=test;Persist Security Info=True;Unicode=true;"), true) {
    }

    static MyContext() {

      Database.SetInitializer<MyContext>(null);
    }

    public DbSet<MyClass> MyClasses { get; set; }
  }

  public class MyContextWithoutXmin: MyContext {

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {

      modelBuilder.Entity<MyClass>().Ignore(p => p.xmin);
    }

  }
Example of usage

Code: Select all

     using (var ctx = new MyContextWithoutXmin()) {
        if (!ctx.Database.Exists())
          ctx.Database.Create();
      }

      using (var ctx = new MyContext()) {
        ctx.MyClasses.Add(new MyClass() { FStringField = "text" });
        ctx.MyClasses.Add(new MyClass() { FStringField = "another text" });
        ctx.SaveChanges();
      }

      using (var ctx1 = new MyContext())
      using (var ctx2 = new MyContext()) {
        var item1 = ctx1.MyClasses.Where(t => t.FStringField == "text").FirstOrDefault();
        var item2 = ctx2.MyClasses.Where(t => t.FStringField == "text").FirstOrDefault();
        item1.FStringField = "text_mod";
        item2.FStringField = "text_mod";
        ctx1.SaveChanges();
        ctx2.SaveChanges(); // the expected concurrency error
      }

[email protected]
Posts: 2
Joined: Fri 25 Jan 2019 03:15

Re: How to set Optimistic Concurrency, Entity Framework 6

Post by [email protected] » Mon 18 Feb 2019 02:58

Thank you for your help.
It worked very well !

Post Reply