使用EF Core递增字母数字字符串属性
项目的概述
我有一个.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论