I have to charge my users a monthly fee and want to make sure I don’t charge them twice.
So 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?
<code>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
await _dbContext.PlatformFee.Add(platformFee);
await _dbContext.SaveChangesAsync();
// pay fee and move on
} catch(DbUpdateConcurrencyException ex) {
}
} else {
// don't charge fee and do something else
}
</code>
<code>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
await _dbContext.PlatformFee.Add(platformFee);
await _dbContext.SaveChangesAsync();
// pay fee and move on
} catch(DbUpdateConcurrencyException ex) {
}
} else {
// don't charge fee and do something else
}
</code>
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
await _dbContext.PlatformFee.Add(platformFee);
await _dbContext.SaveChangesAsync();
// pay fee and move on
} catch(DbUpdateConcurrencyException ex) {
}
} else {
// don't charge fee and do something else
}