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.
How to set Optimistic Concurrency, Entity Framework 6
-
- Posts: 2
- Joined: Fri 25 Jan 2019 03:15
Re: How to set Optimistic Concurrency, Entity Framework 6
We will investigate the question and notify you about the result.
Re: How to set Optimistic Concurrency, Entity Framework 6
Here is an example of setting Optimistic Concurrency with Entity Framework 6 via dotConnect for PostgreSQL.
Class
Contexts (MyContextWithoutXmin is required only for the Database.Create() functionality)
Example of usage
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; }
}
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);
}
}
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
}
-
- Posts: 2
- Joined: Fri 25 Jan 2019 03:15
Re: How to set Optimistic Concurrency, Entity Framework 6
Thank you for your help.
It worked very well !
It worked very well !