我最近使用 Linq 开发了一个 C# 应用程序。我从外部数据库获取需要处理的配置文件列表,有些是新的,有些已经在数据库中,需要更新。我今天要做的是检查配置文件列表并检查每个配置文件是否存在我更新否则我插入 - 这个解决方案工作正常。
我确信有一种方法可以使用批量插入/更新类似 UPDATE ON DUPLICATE 的方法,这样我可以节省时间,因为我得到的文件很大,而且批量插入/更新具有更好的性能。我想避免我现在使用的迭代。
insertall 不适用于已存储的行,我需要更新和插入的组合
这是我的代码,非常感谢您的帮助。
foreach (Profile tmpProfile in profiles)
{
try
{
var matchedProfile = (from c in db.ProfileEntities
where c.ProfileId == tmpProfile.Id
select c).SingleOrDefault();
if (matchedProfile == null)
{
//Insert
db.ProfileEntities.InsertOnSubmit(EntityMapper.ToEntity(tmpProfile));
}
else
{
//Update
EntityMapper.ToEntity(ref matchedProfile, tmpProfile);
}
}
catch (System.Data.SqlServerCe.SqlCeException sqlExec)
{
}
catch (Exception e)
{
}
}
db.SubmitChanges();