在第一个添加方法时,我得到了异常: ObjectContext实例已被释放,不能再用于需要连接的操作。
我知道我需要在using()括号中,并且它是,而Transaction是公共虚拟
public void confirmPayments(List<int> Payments)
{
using (var db = new BankContext())
{
try
{
foreach (int a in Payments)
{
foreach (Payment payment in db.Payment)
{
if (a == payment.Id)
{
Transaction senderTransaction = new Transaction
{
Amount = payment.Amount,
CreationDate = payment.CreationDate,
DueDate = payment.DueDate,
Receiver = payment.Receiver,
Sender = payment.Sender,
KIDMessage = payment.KIDMessage
};
Transaction receiverTranscation = new Transaction
{
Amount = payment.Amount,
CreationDate = payment.CreationDate,
DueDate = payment.DueDate,
Receiver = payment.Receiver,
Sender = payment.Sender,
KIDMessage = payment.KIDMessage
};
Account senderAccount = findAccount(senderTransaction.Sender);
Account receiverAccount = findAccount(receiverTranscation.Receiver);
senderAccount.Transaction.Add(senderTransaction);
senderAccount.Payment.Remove(payment);
senderAccount.Balance = senderAccount.Balance - senderTransaction.Amount;
receiverAccount.Transaction.Add(receiverTranscation);
receiverAccount.Balance = receiverAccount.Balance + receiverTranscation.Amount;
}
}
}
db.SaveChanges();
}
catch (Exception error)
{
System.Diagnostics.Debug.WriteLine("FAILED TO CONFIRM PAYMENT: \n" + error.Message + "\n");
}
}
}
我想加入的表/类(codefirst):
public class Account
{
[Key]
public int Id { get; set; }
public double Balance { get; set; }
public string AccountNumber { get; set; }
public string AccountType { get; set; }
public string CreationDate { get; set; }
public string ClosingDate { get; set; }
public virtual List<Transaction> Transaction { get; set; }
public virtual List<Payment> Payment { get; set; }
}
答案 0 :(得分:0)
findAccount()也在使用(var db = New BankContext())我觉得与另一个使用来自confirmPayments()的混淆。