实体框架插入多个表 - 不插入外键

时间:2014-01-19 20:26:10

标签: entity-framework

我想同时插入两个表。

代码:

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: enter image description here

据我所知,我在这里连接实体: transaction.PaymentTransaction = paymentTransaction;

在payTransaction被提交后,它会自动将外键添加到Transaction,但它没有。

0 个答案:

没有答案