存储库+ UnitOfWork + EF更新详细记录时更新主记录。 (TotalAmount =总和(金额)) [英] Repository + UnitOfWork + EF Updating Master Record when detail records changed. (TotalAmount = Sum(Amount))

查看:110
本文介绍了存储库+ UnitOfWork + EF更新详细记录时更新主记录。 (TotalAmount =总和(金额))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Entity Framework实现Repository和UnitOfWork模式。

Hi, i'm trying to implement a Repository and UnitOfWork patterns using Entity Framework.

然后我执行CreateOrUpdateMultiple()。 CreateMultiple将新记录添加到存储库。 UpdateMultiple检索要更新的记录。

Then i perform a CreateOrUpdateMultiple(). CreateMultiple add the new records to the repository. UpdateMultiple retrieve records that going to be updated.

完成两个操作后,我需要用包含所有详细记录的sum(字段)更新主记录。 (我的意思是现有的未经修改的内存和内存中的那些)

After the two operations completes i need to update the master record with a sum(field) with all detail records. (With all i mean the existing ones that were not modified and the ones in memory)

这是我到目前为止所想的......

This is what i have thought so far...



  1. 我应该检索所有详细记录,然后将所有内容混合在一起(修改与否以及添加的列表),然后进行总和操作?

  1. should i retrieve all detail records and then mix all in one list (modified or not and the addded ones), and then do the sum operation?

只从数据库中读取要更新的记录(认为这样会更快,因为如果我有40条记录,只有3条被修改,2条被添加,我将无法阅读整套)然后
以某种方式执行主记录的更新,但问题是这些记录还没有存在于数据库中。

Read from database only records to be updated (thinking this will be faster because if i have 40 records and just 3 are modified and 2 added i will not read the entire set) and then somehow perform the update to the master record, but the problem is those records aren't yet in the database.

我只有一个ObjectContext实例用于所有操作,我在我的服务中调用SaveChanges()来在一次交易中提交所有操作。

I have just one ObjectContext instance for all operations and i call SaveChanges() in my service to commit all in just one transaction.

是我现在使用transactionScope ...这是我想避免的,因为所有对数据库的调用



The is what i have right now using transactionScope... and this is what i'm trying to avoid because of all the calls to database


//Service Layer
Method()
{
  Method1.Invoke(masterRecordId, detaildRecords); //
}

//Business Layer
Method1(masterRecordId, detailRecords)
{
   using(TransactionScope ts = new TransactionScope())
   {
		 var recordsToUpdate = dal.RetrieveOnlyRecordsToUpdate();
		 
		 //Update retrieved records with the values of recods comming from the client
		 dal.Update(recordsToUpdate); //ctx.ApplyChanges(); and ctx.SaveChanges();
		 
		 dal.Add(recordsToAdd) //ctx.Add(detail records); and ctx.SaveChanges();
     
		 //Update master record TotalSum
		 dal.UpdateMasterRecord(masterRecordId); //Here is performed ctx.ExecuteStoredCommand("UPDATE MasterTable = SUM() WHERE MasterRecordId = {0}")...
		 
     Method2();
     
     ts.Complete();
   }
}

Method2(masterRecordId)
{
   using(TransactionScope ts = new TransactionScope())
   {
	   MasterRecord m = Retrieve(masteRecordId);
     Notification notification = new Notification(){ ...assign properties..., m.TotalSum};
		
		 dal.Add(notification); //ctx.Add(notification); and ctx.SaveChanges();
          
     ts.Complete(); 
   }
}

推荐答案

据我所知,你可以参考这个文章,讨论更新主 - 细节实体。

As I know, you can refer to this article, which talks about the updating master-detail entity.

这是链接:

http://blogs.msdn.com/b/bethmassi/archive/2009/07/14/master-detail-data-binding-in-wpf-with-entity-framework.aspx?PageIndex=2&title = TITLE_REPLACE& t = TITLE_REPLACE& title = TITLE_REPLACE& h = TITLE_REPLACE& s =& t = TITLE_REPLACE

希望有所帮助,


这篇关于存储库+ UnitOfWork + EF更新详细记录时更新主记录。 (TotalAmount =总和(金额))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆