使用EF Core递增字母数字字符串属性

发布于 2025-02-06 00:32:01 字数 1402 浏览 2 评论 0原文

项目的概述

我有一个.NET Core 6 Web API项目,该项目将PostgreSQL用作数据库和EF Core 6作为连接到数据库的ORM。该项目已设置为使用代码第一方法。

问题语句

项目“事务”中的实体之一具有名称为“ coreencecode”(不是主键)的alpha数字字符串属性。参考代码的格式为“ TR00001”,“ TR00002”,其中每个事务对象创建的值必须增加。

public class Transaction : AuditableEntity<Guid>
{
    public Guid AccountId { get; private set; }
    public string? ReferenceCode { get; private set; } //Property of concern
    public TransactionType TransactionType { get; private set; }
    public decimal Amount { get; private set; }
    public string? UserComments { get; private set; }
    public string? TransactionComments { get; private set; }
    public DateTime MadeOn { get; private set; }
}

目前,我采用了如图所示的代码内部手动中的值的方法。

var currentCount = this.applicationDbContext.Transactions.Count(); //created entries will not be deleted from database
var referenceCode = "TR" + string.Format("{0:0000000000}", currentCount + 1);
var transactionInToAccount = new Transaction(Guid.NewGuid(), request.ToAccountId, referenceCode, Domain.Transaction.TransactionType.Deposit, request.Amount, request.UserComments, request.TransactionComments, request.MadeOn, this.currentUser.GetUserId());

当我们同时提出交易创建请求时,该代码通过为不同的交易对象创建相同的参考代码来引起问题。那么,有没有办法在代码第一种方法中解决此问题?

Overview of the project

I have a .net core 6 web api project which uses postgresql as the database and ef core 6 as the ORM to connect to the database. The project is setup to use code first approach.

Problem Statement

One of the entities in the project "Transaction" has a alpha numeric string property by the name "ReferenceCode" (not a primary key). The reference code has the format of "TR00001", "TR00002" where in the value has to increase for every transaction object creation.

public class Transaction : AuditableEntity<Guid>
{
    public Guid AccountId { get; private set; }
    public string? ReferenceCode { get; private set; } //Property of concern
    public TransactionType TransactionType { get; private set; }
    public decimal Amount { get; private set; }
    public string? UserComments { get; private set; }
    public string? TransactionComments { get; private set; }
    public DateTime MadeOn { get; private set; }
}

Currently I have employed the approach of incrementing the value manually inside of the code as shown inline.

var currentCount = this.applicationDbContext.Transactions.Count(); //created entries will not be deleted from database
var referenceCode = "TR" + string.Format("{0:0000000000}", currentCount + 1);
var transactionInToAccount = new Transaction(Guid.NewGuid(), request.ToAccountId, referenceCode, Domain.Transaction.TransactionType.Deposit, request.Amount, request.UserComments, request.TransactionComments, request.MadeOn, this.currentUser.GetUserId());

This code is causing problems by creating same reference code for different transaction objects when we have concurrent requests for transaction creation. So is there a way to solve this problem in code first approach?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文