【发布时间】:2017-02-01 01:54:17
【问题描述】:
只是对我要完成的工作的一些概述。 我们在应用程序中保留远程数据库(第 3 方)的本地副本。要下载信息,我们使用 api。 我们目前按计划下载信息,然后将新记录插入本地数据库或更新现有记录。 这是它目前的工作方式
public void ProcessApiData(List<Account> apiData)
{
// get the existing accounts from the local database
List<Account> existingAccounts = _accountRepository.GetAllList();
foreach(account in apiData)
{
// check if it already exists in the local database
var existingAccount = existingAccounts.SingleOrDefault(a => a.AccountId == account.AccountId);
// if its null then its a new record
if(existingAccount == null)
{
_accountRepository.Insert(account);
continue;
}
// else its a new record so it needs updating
existingAccount.AccountName = account.AccountName;
// ... continue updating the rest of the properties
}
CurrentUnitOfWork.SaveChanges();
}
这很好用,但感觉可以改进。
- 每个实体都有这些方法之一,它们都做同样的事情(只是更新不同的属性)或插入不同的实体。有没有办法让它更通用?
- 似乎有很多数据库调用,无论如何“批量”执行此操作。我看过这个包,我在其他一些帖子中提到过这个包https://github.com/loresoft/EntityFramework.Extended 但它似乎专注于批量更新具有相同值的单个属性,或者我可以说。
任何关于我如何改进这一点的建议都会很棒。我对 c# 还很陌生,所以我仍在寻找最好的做事方式。
我使用 .net 4.5.2 和 Entity Framework 6.1.3 以及 MSSQL 2014 作为后端数据库
【问题讨论】:
标签: c# sql-server entity-framework