Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core with EF Core and CosmosDB - IdentityRole issue

I am trying to migrate my asp.net core application from using Azure SQL database to use CosmosDB database.

Facing an issue when trying to create collections with the following error messages:

System.InvalidOperationException: 'The entity type 'IdentityRole' has property 'ConcurrencyStamp' as its concurrency token, but only '_etag' is supported. Consider using 'EntityTypeBuilder.UseETagConcurrency'.'

I have tried these changes, none of them solve the issue.

  1. I added UseETagConcurrency when building the model builder.Entity<IdentityRole>().ToContainer("Roles").UseETagConcurrency();
  2. I create new derived class from IdentityRole, added _etag property to Role model

Any ideas on how to resolve this issue?

Thank you for your assistance.

like image 475
Joko Supriyanto Avatar asked Aug 06 '20 21:08

Joko Supriyanto


People also ask

When should you not use Efcore?

One of the biggest reasons not to use Entity Framework Core is that your application needs the fastest possible data access. Some applications do a lot of heavy data operations with very high-performance demands, but usually business applications don't have that high of a performance demand.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Is EF core faster than EF6?

EF Core 6.0 performance is now 70% faster on the industry-standard TechEmpower Fortunes benchmark, compared to 5.0. This is the full-stack perf improvement, including improvements in the benchmark code, the . NET runtime, etc. EF Core 6.0 itself is 31% faster executing queries.


2 Answers

 protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.Entity<IdentityRole>()
            .Property(b => b.ConcurrencyStamp)
            .IsETagConcurrency();
        builder.Entity<ApplicationUser>() // ApplicationUser mean the Identity user 'ApplicationUser : IdentityUser'
            .Property(b => b.ConcurrencyStamp)
            .IsETagConcurrency();

    }
like image 174
Sachithamh Avatar answered Oct 19 '22 03:10

Sachithamh


Figured it out! The issue is Role & User already have a concurrency token which is stored in the property 'ConcurrencyStamp'. So we need to tell EF that this property is what we want to use for the Etag concurrency. So instead of using UseETagConcurrency() use the following:

builder.Property(d => d.ConcurrencyStamp)
       .IsETagConcurrency();
like image 34
YodasMyDad Avatar answered Oct 19 '22 03:10

YodasMyDad