【问题标题】:ASP.Net Core with EF Core and CosmosDB .NET 5 - IdentityRole issue带有 EF Core 和 CosmosDB .NET 5 的 ASP.Net Core - IdentityRole 问题
【发布时间】:2021-06-04 08:43:51
【问题描述】:

我的问题和这个类似。

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

提供的答案不起作用。它会产生其他问题。一旦我将应用程序从 .NET Core 3.1 升级到 .NET 5,这些错误就会开始出现。降级应用程序会使所有错误都消失。显然,这个问题与 efcore 5 中引入的对 cosmos 的乐观并发支持有关。

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

任何帮助将不胜感激。

【问题讨论】:

  • 先生您好,根据您的描述,我认为您的问题是当您将 core3.1 升级到 5 时,遇到错误 'System.InvalidOperationException: 'The entity type 'IdentityRole' has属性“ConcurrencyStamp”作为其并发标记,但仅支持“_etag”。考虑使用“EntityTypeBuilder.UseETagConcurrency”。 ' ??
  • 正确。这是我得到的错误。正如我上面提到的帖子中提到的,我尝试应用:``` builder.Property(d => d.ConcurrencyStamp) .IsETagConcurrency(); ``` 但是在应用这个之后,我的身份提供者给出了并发错误,我无法登录。
  • 我不确定,但也许你可以看看this sample。我对它进行了测试,效果很好。
  • 您有什么进展吗先生?如果存在,请随时分享您的进一步问题。

标签: entity-framework-core asp.net-identity azure-cosmosdb


【解决方案1】:

我认为this sample 可以帮助解决您的问题,我在我身边进行了测试,效果很好。

<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
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-11-27
    • 1970-01-01
    • 2021-07-22
    • 2020-02-01
    • 2019-05-15
    • 2018-11-12
    • 1970-01-01
    • 2017-12-02
    • 2017-01-19
    相关资源
    最近更新 更多