【问题标题】:ASP.Net Core with EF Core and CosmosDB - IdentityRole issue带有 EF Core 和 CosmosDB 的 ASP.Net Core - IdentityRole 问题
【发布时间】:2020-11-27 04:42:45
【问题描述】:

我正在尝试将我的 asp.net 核心应用程序从使用 Azure SQL 数据库迁移到使用 CosmosDB 数据库。

在尝试创建包含以下错误消息的集合时遇到问题:

System.InvalidOperationException: '实体类型'IdentityRole'有 属性 'ConcurrencyStamp' 作为其并发标记,但只有 '_etag' 支持。考虑使用 'EntityTypeBuilder.UseETagConcurrency'。'

我已经尝试了这些更改,但都没有解决问题。

  1. 我在构建模型时添加了UseETagConcurrency builder.Entity<IdentityRole>().ToContainer("Roles").UseETagConcurrency();
  2. 我从IdentityRole 创建了新的派生类,添加了_etag Role 模型的属性

关于如何解决此问题的任何想法?

感谢您的帮助。

【问题讨论】:

  • 我也有同样的问题。

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


【解决方案1】:
 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();

    }

【讨论】:

    【解决方案2】:

    想通了!问题是角色和用户已经有一个并发令牌,它存储在属性“ConcurrencyStamp”中。所以我们需要告诉 EF 这个属性是我们想要用于 Etag 并发的。因此,不要使用UseETagConcurrency(),而是使用以下内容:

    builder.Property(d => d.ConcurrencyStamp)
           .IsETagConcurrency();
    

    【讨论】:

    • 谢谢。它解决了创建集合的问题。我现在在尝试通过调用 userManager.AddToRoleAsync 向用户添加角色时遇到问题。我收到错误 Microsoft.Azure.Cosmos.CosmosException :响应状态代码不表示成功:NotFound (404);子状态:0;活动编号:ff3980f5-90ab-4dd1-be48-e28a2b8a8575;原因:({ "Errors": [ "Resource Not Found. Learn more: aka.ms/cosmosdb-tsg-not-found" ] });
    • 我使用 context.UserRole 作为我的解决方法填充了 UserRoles,发现另一个问题,用户无法签名我 LINQ 表达式 'DbSet>() .Join( inner: DbSet (), outerKeySelector: i => i.RoleId, innerKeySelector: i0 => i0.Id, resultSelector: (i, i0) => new TransparentIdentifier, IdentityRole>( Outer = i, Inner = i0 ))' 无法翻译
    • 这可能不是一个好的答案。 ConcurrencyStamp 已被 MS Identity 使用,并且可能与 SQL 服务器行版本耦合。 Etag 特定于 Azure Cosmos DB。添加此行可能会遇到并发问题,因为单独的包都会更新属性。我已经在 SO 上看到了两个问题1、2。
    猜你喜欢
    • 2021-06-04
    • 2019-05-15
    • 2018-11-12
    • 1970-01-01
    • 2017-12-02
    • 2017-01-19
    • 1970-01-01
    • 2021-12-18
    • 2020-11-20
    相关资源
    最近更新 更多