I have to charge my users a monthly fee and want to make sure I don't charge them twice.
I created a table called PlatformFee that looks like this to check to see if I've charged them. If there's any entry with the ReceiverId and current month (date as 0624), then I don't charge, otherwise I charge a fee.
Can I set a ConcurrencyToken on both the the ReceiverID and Date so that if another entry tries to insert itself, it will throw a DbUpdateConcurrencyException? How would I do that?
2nd question - would I want a concurrency token or a unique key on both the ReceiverId and Date? Or both?
public class PlatformFee : BaseEntity
{
public int ReceiverId { get; set; }
public long Fee { get; set; }
// Use 4 digit as date Ex. MMYY, 0524
public int Date { get; set; }
}
var hasPaidMonthlyFee = await _dbContext.PlatforFee.Where(p => p.ReceiverId == id && p.Date == 0624).Any();
if (hasPaidMonthlyFee == null)
{
try
{
// insert new record into table
_dbContext.PlatformFee.Add(platformFee);
await _dbContext.SaveChangesAsync();
// pay fee and move on
}
catch(DbUpdateConcurrencyException ex)
{
}
}