实体框架核心多租户:基于另一列值的自动增量列
使用实体框架核心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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过为每个租户拥有一个差异顺序来实现这一目标,并将其作为租户入职的一部分脚本。
前任:
当您创建租户时,您将运行以下脚本:
当您将AR Ecord插入示例表时,您将必须称此脚本TP获取partitionId的下一个值,
以此方式,每个租户都应该拥有自己的序列
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:
When you insert ar ecord to the sample table, you will have to call this script tp get the next value for PartitionId
This way every tenant should have its own sequence
不起作用,已经创建了序列
请注意,我正在使用Postgres SQL,
在我的情况下,选择
语句的下一个值不正确的语法。请在下面找到我的更改。
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.