Entity framework 实体框架插入多个表-不插入外键
我想同时插入两个表 代码:Entity framework 实体框架插入多个表-不插入外键,entity-framework,Entity Framework,我想同时插入两个表 代码: public void AddPaymentTransactions(列表、int customerId、十进制余额) { foreach(列表中的变量项) { PaymentTransaction PaymentTransaction=新的PaymentTransaction(); paymentTransaction.CustomerId=CustomerId; paymentTransaction.Amount=item.ItemTotal; paymentTr
public void AddPaymentTransactions(列表、int customerId、十进制余额)
{
foreach(列表中的变量项)
{
PaymentTransaction PaymentTransaction=新的PaymentTransaction();
paymentTransaction.CustomerId=CustomerId;
paymentTransaction.Amount=item.ItemTotal;
paymentTransaction.OfferId=item.OfferId;
//对于每个付款交易,我们添加交易事件
事务=新事务();
transaction.CustomerId=CustomerId;
交易状态=1;
transaction.Description=“为“+item.OfferName+”付款”;
transaction.TransactionType=1;
transaction.Credit=item.ItemTotal;
transaction.Balance=余额+item.ItemTotal;
transaction.PaymentTransaction=PaymentTransaction;
this.ClientRepositories.LiveData.AddToPaymentTransactions(paymentTransaction);
this.ClientRepositories.LiveData.AddToTransactions(事务);
}
this.ClientRepositories.LiveData.SaveChanges();
}
DB:
据我所知,我在此连接实体:
transaction.PaymentTransaction=PaymentTransaction
在paymentTransaction提交后,它会自动将外键添加到事务中,但不会。是的,它应该这样做,但在
LiveData
中会发生什么?(顺便说一句,我不会在数据库中存储特定于UI的显示元数据,即HTML标记)。LiveData只是web数据服务的代理。我不明白。因为您使用了标记“mvc”,所以您的代码看起来已经像服务器端代码了。LiveData
的(基本)类型是什么?它是服务器端。LiveData是web数据服务的代理。。。它是实体的代理。您的程序的体系结构是什么?您是说您使用的是实体框架,并显示调用this.ClientRepositories.LiveData上的方法的代码,但是否说LiveData是指向web服务的指针?您需要显示Web服务针对EF调用的代码。
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();
}