igg*_*web 17 c# entity-framework
有人可以告诉我如何检查记录是否存在,如果它确实存在然后什么也不做,如果没有,那么将记录添加到数据库中?
请参阅下面的代码:
if (isIpnValidated == true)
{
using (WebApplication1Entities db = new WebApplication1Entities())
{
Orders order = new Orders();
order.UserId = userId;
order.Date = System.DateTime.Now;
order.Transaction = txnId;
order.Amount = Convert.ToDecimal(mcGross);
order.Email = payerEmail;
order.Country = residenceCountry;
db.Orderss.Add(order);
db.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想确保数据库中没有可能的重复.
sro*_*oes 34
用途Any:
if (isIpnValidated == true)
{
using (WebApplication1Entities db = new WebApplication1Entities())
{
if (db.Orderss.Any(o => o.Transaction == txnId)) return;
Orders order = new Orders();
order.UserId = userId;
order.Date = System.DateTime.Now;
order.Transaction = txnId;
order.Amount = Convert.ToDecimal(mcGross);
order.Email = payerEmail;
order.Country = residenceCountry;
db.Orderss.Add(order);
db.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71106 次 |
| 最近记录: |