Umer Nawab
Umer Nawab

Reputation: 21

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

My Question is similar to this one.

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

The provided answer does not work. It creates other issues. These errors started appearing once i upgraded my app from .NET Core 3.1 to .NET 5. Downgrading the app made all errors dissapear. Apparently the issue is related to the optimistic concurrency support for cosmos introduced in efcore 5.

https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#cosmos

Any help would be appreciated.

Upvotes: 0

Views: 474

Answers (1)

Tiny Wang
Tiny Wang

Reputation: 15906

I think this sample could help to solve your problem, I tested it in my side and it did work well.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="5.0.2" />
  </ItemGroup>

</Project>



using Microsoft.EntityFrameworkCore;

namespace Cosmos.ModelBuilding
{
    public class OrderContext : DbContext
    {
        public DbSet<Order> Orders { get; set; }
        public DbSet<Distributor> Distributors { get; set; }

        #region Configuration
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseCosmos(
                "endpoint",
                "primarykey",
                databaseName: "Tasks");

        #endregion

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region DefaultContainer
            modelBuilder.HasDefaultContainer("Store");
            #endregion

            #region Container
            modelBuilder.Entity<Order>()
                .ToContainer("Orders");
            #endregion

            #region NoDiscriminator
            modelBuilder.Entity<Order>()
                .HasNoDiscriminator();
            #endregion

            #region PartitionKey
            modelBuilder.Entity<Order>()
                .HasPartitionKey(o => o.PartitionKey);
            #endregion

            #region ETag
            modelBuilder.Entity<Order>()
                .UseETagConcurrency();
            #endregion

            #region PropertyNames
            modelBuilder.Entity<Order>().OwnsOne(
                o => o.ShippingAddress,
                sa =>
                {
                    sa.ToJsonProperty("Address");
                    sa.Property(p => p.Street).ToJsonProperty("ShipsToStreet");
                    sa.Property(p => p.City).ToJsonProperty("ShipsToCity");
                });
            #endregion

            #region OwnsMany
            modelBuilder.Entity<Distributor>().OwnsMany(p => p.ShippingCenters);
            #endregion

            #region ETagProperty
            modelBuilder.Entity<Distributor>()
                .Property(d => d.ETag)
                .IsETagConcurrency();
            #endregion
        }
    }
}

Upvotes: 2

Related Questions