0
最初の追加メソッドで例外が発生しました。 ObjectContextインスタンスが破棄され、接続が必要な操作に使用できなくなりました。System.ObjectDisposedExceptionが表示され、何も見つからないようです。
私は私が使用して()カッコ内にある必要知っているし、それがある、との取引は、パブリック仮想
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; }
}
あなたが呼んでいる機能(特にfindAccount)は非常に疑わしく見えます。デバッガを接続し、デバッガがスローする実際の場所を取得できるはずです。 – BradleyDotNET
'findAccount()'とは何ですか?まったく異なるDBコンテキストからオブジェクトを取得し、そのコンテキストを破棄していますか? – David
@Davidありがとうございました!これはうまくいきました.. findAccount()が同じデータベースを使用しているので問題ではないと思いました... –