我想同时插入两个表。
代码:
public void AddPaymentTransactions(List<CartItemModel> list, int customerId, decimal balance)
{
foreach (var item in list)
{
PaymentTransaction paymentTransaction = new PaymentTransaction();
paymentTransaction.CustomerId = customerId;
paymentTransaction.Amount = item.ItemTotal;
paymentTransaction.OfferId = item.OfferId;
// for each payment transaction we add transaction event
Transaction transaction = new Transaction();
transaction.CustomerId = customerId;
transaction.Status = 1;
transaction.Description = "Payment for <b>" + item.OfferName + "</b>";
transaction.TransactionType = 1;
transaction.Credit = item.ItemTotal;
transaction.Balance = balance + item.ItemTotal;
transaction.PaymentTransaction = paymentTransaction;
this.ClientRepositories.LiveData.AddToPaymentTransactions(paymentTransaction);
this.ClientRepositories.LiveData.AddToTransactions(transaction);
}
this.ClientRepositories.LiveData.SaveChanges();
}
DB:
据我所知,我在这里连接实体: transaction.PaymentTransaction = paymentTransaction;
在payTransaction被提交后,它会自动将外键添加到Transaction,但它没有。