如何在1种方法中使用2个DB上下文? C#

时间:2016-06-30 10:02:54

标签: c# entity-framework model-view-controller entity-framework-5 dbcontext

我正在编写一种方法来为每个成员保存发票,发票和成员是具有不同存储库的不同实体。我怎样才能利用这两种情境?我应该处理成员存储库吗?

 public RedirectToRouteResult CalculateAllInvoices()
        {
            var amount = _memberRepository.GetAll().Count();

            for (int i = 1; i<amount; i++)
            {

                Member m = _memberRepository.GetSingle(i);


                CalculateInvoice(m);

            }
            return RedirectToAction("Overview");
        }

        private void CalculateInvoice(Member member)
        {
            double invoicePrice = 0.0;
            ICollection<Sport> sports = member.Sports;

            foreach(Sport s in sports){
               invoicePrice =+ s.Price;
            }

            Invoice i = new Invoice { SendDate = DateTime.Now, Status = false, Member = member, Price = invoicePrice };

           _memberRepository.Dispose();
            _InvoiceRepository.Add(i);
            _InvoiceRepository.Save();
        }

0 个答案:

没有答案