具有多个序列的 EF Core 6 HasDefaultValueSql
我目前正在重写一个旧服务,并遇到了一个问题。
在 SQL Server 中,我们为每个客户端产品编号都有一个序列。旧版本使用存储过程来创建产品。但我正在研究是否可以使用 HasDefaultValueSql
做一些事情。
MS文档说使用.HasDefaultValueSql("NEXT VALUE FOR sequence")
,但是有没有办法根据client_id列区分使用哪个序列?
我的产品实体类型配置:
builder.ToTable("products");
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).HasColumnName("product_id")
.ValueGeneratedOnAdd();
builder.Property(e => e.ClientId).HasColumnName("client_id");
builder.Property(e => e.Number).HasColumnName("number")
.HasDefaultValueSql("NEXT VALUE FOR client_idProductNumberSequence");
builder.Property(e => e.Name).HasColumnName("name");
builder.Property(e => e.Description).HasColumnName("description");
...other props
builder.HasIndex(e => new { e.ClientId, e.Number })
.IsUnique(true);
如果不可能,解决此问题的最佳方法是什么?
I'm currently re-writing an old service, and ran into an issue.
In the SQL Server we have a sequence for each clients product number. The old version used stored procedures for creating products. But I was looking into if it was possible to do something with HasDefaultValueSql
.
The MS docs said to use .HasDefaultValueSql("NEXT VALUE FOR sequence")
, but is there some way to differentiate which sequence to use based on the client_id column?
My product entity type configuration:
builder.ToTable("products");
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).HasColumnName("product_id")
.ValueGeneratedOnAdd();
builder.Property(e => e.ClientId).HasColumnName("client_id");
builder.Property(e => e.Number).HasColumnName("number")
.HasDefaultValueSql("NEXT VALUE FOR client_idProductNumberSequence");
builder.Property(e => e.Name).HasColumnName("name");
builder.Property(e => e.Description).HasColumnName("description");
...other props
builder.HasIndex(e => new { e.ClientId, e.Number })
.IsUnique(true);
If thats not possible, what would be the best way to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论