实体框架核心多租户:基于另一列值的自动增量列

发布于 2025-01-24 21:16:25 字数 1154 浏览 4 评论 0原文

使用实体框架核心6与样本类别

public class Sample
{
  [Key]
  public long Id { get; set; }
  public long TenantId { get; set; }
  public long PartitionId { get; set; } 
}

I需要在品牌ID中自动增加分区ID。

首先,我在tenantid和partitionID之间添加索引映射器,以确保与partitionId valuegeneratedOnadd()函数中使用过的不重复,我缺少的部分如何使自动生成partitionId foreach foreach foreach foreach tenantid。

        modelBuilder.Entity<Sample>(builder =>
        {

            // enable unique partion id based in tenant level
            builder.HasIndex(r => new { r.PartitionId, r.TenantId }).HasDatabaseName("IX_Sample_PartitionId").IsUnique();

            builder.ToTable("Sample").Property(p => p.Id).HasColumnOrder(0);

            builder.ToTable("Sample").Property(p => p.TenantId ).HasColumnOrder(1);

            // create auto increment base in brand id
            builder.ToTable("Sample").Property(p => p.PartitionId).ValueGeneratedOnAdd().HasColumnOrder(2);

       });

所需的结果应该是这样

Id | TenantId | PartitionId 
1  | 1        | 1
2  | 1        | 2
3  | 2        | 1
4  | 2        | 2

Using entity framework core 6 with sample class

public class Sample
{
  [Key]
  public long Id { get; set; }
  public long TenantId { get; set; }
  public long PartitionId { get; set; } 
}

I need to make auto increment of partition id based in brand id.

First I add index mapper between tenantId and partitionId to make sure never duplicated than i used in partitionId ValueGeneratedOnAdd() function, my missing part how i can make auto generation of partitionId foreach tenantId in sequence.

        modelBuilder.Entity<Sample>(builder =>
        {

            // enable unique partion id based in tenant level
            builder.HasIndex(r => new { r.PartitionId, r.TenantId }).HasDatabaseName("IX_Sample_PartitionId").IsUnique();

            builder.ToTable("Sample").Property(p => p.Id).HasColumnOrder(0);

            builder.ToTable("Sample").Property(p => p.TenantId ).HasColumnOrder(1);

            // create auto increment base in brand id
            builder.ToTable("Sample").Property(p => p.PartitionId).ValueGeneratedOnAdd().HasColumnOrder(2);

       });

Required result should be like this

Id | TenantId | PartitionId 
1  | 1        | 1
2  | 1        | 2
3  | 2        | 1
4  | 2        | 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2025-01-31 21:16:25

您可以通过为每个租户拥有一个差异顺序来实现这一目标,并将其作为租户入职的一部分脚本。

前任:
当您创建租户时,您将运行以下脚本:

CREATE SEQUENCE [dbo].[Tenant{0}_PartionIdSequence] AS [int] START WITH 1 INCREMENT BY 1

当您将AR Ecord插入示例表时,您将必须称此脚本TP获取partitionId的下一个值,

SELECT NEXT VALUE FOR dbo.[Tenant{0}_PartionIdSequence]

以此方式,每个租户都应该拥有自己的序列

You can achieve this by having a difference sequence for each tenant and you script it as part of the tenant onboarding.

ex:
When you create a tenant, you run the following script:

CREATE SEQUENCE [dbo].[Tenant{0}_PartionIdSequence] AS [int] START WITH 1 INCREMENT BY 1

When you insert ar ecord to the sample table, you will have to call this script tp get the next value for PartitionId

SELECT NEXT VALUE FOR dbo.[Tenant{0}_PartionIdSequence]

This way every tenant should have its own sequence

骷髅 2025-01-31 21:16:25

不起作用,已经创建了序列
请注意,我正在使用Postgres SQL,在我的情况下,选择语句的下一个值不正确的语法。

请在下面找到我的更改。

   // create sequence for partition id and  map it in tenant level
   modelBuilder.HasSequence<long>("Tenant{0}_PartitionId_seq")
               .StartsAt(1)
               .IncrementsBy(1);

   modelBuilder.Entity<Sample>(builder =>
   {
        // enable unique partion id based in tenant level
        builder.HasIndex(r => new { r.PartitionId, r.TenantId })
               .HasDatabaseName("IX_Sample_PartitionId")
               .IsUnique();

       builder.ToTable("Sample").Property(p => p.Id).HasColumnOrder(0);
       builder.ToTable("Sample").Property(p => p.TenantId).HasColumnOrder(1);

       // mapping sequence table which consider tenant id
       builder.ToTable("Sample").Property(p => p.PartitionId)
             .HasDefaultValueSql("nextval('\"Tenant{0}_PartitionId_seq\"')")
             .HasColumnOrder(2);
   });

Not working, sequence has been created
please note i'm using Postgres SQL, SELECT NEXT VALUE FOR statement not correct syntax in my case.

Please find below my changes.

   // create sequence for partition id and  map it in tenant level
   modelBuilder.HasSequence<long>("Tenant{0}_PartitionId_seq")
               .StartsAt(1)
               .IncrementsBy(1);

   modelBuilder.Entity<Sample>(builder =>
   {
        // enable unique partion id based in tenant level
        builder.HasIndex(r => new { r.PartitionId, r.TenantId })
               .HasDatabaseName("IX_Sample_PartitionId")
               .IsUnique();

       builder.ToTable("Sample").Property(p => p.Id).HasColumnOrder(0);
       builder.ToTable("Sample").Property(p => p.TenantId).HasColumnOrder(1);

       // mapping sequence table which consider tenant id
       builder.ToTable("Sample").Property(p => p.PartitionId)
             .HasDefaultValueSql("nextval('\"Tenant{0}_PartitionId_seq\"')")
             .HasColumnOrder(2);
   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文