EFUpsert
这是一个在Entity Framework中实现的简单更新/插入(Upsert)函数。
引言
这是一个在Entity Framework中实现的简单更新/插入(Upsert)函数。
你是否尝试过使用Entity Framework更新或插入数据库记录? 考虑到存在 AddOrUpdate
(using systems.migration 命名空间
),这其实很简单,但它有一些缺点。
我做的是创建一个具有几乎相同功能的简单函数,并且速度足够快以处理重复的数据库往返。 这基本上是在我之前更新近百万条批量数据的问题中测试的。 因此,与其为数据库中的现有记录添加 try catch
和键验证,不如使用这个方法。
这是这段代码最重要的部分,它跟踪本地记录并防止冗余记录传递到本地缓存并插入数据库。
dbSet.Local.FirstOrDefault(expression);
以及这个,查询数据库中的记录
dbSet.SingleOrDefault(expression);
该函数定义了来自你的DB集合的DB上下文
FieldInfo fieldInfoSet = dbSet
.GetType()
.GetField(_set, KeyPropertyBindingFlags);
FieldInfo fieldInfoContext = fieldInfoSet.GetValue(dbSet)
.GetType().BaseType.GetField(_context, KeyPropertyBindingFlags);
return (DbContext)(fieldInfoContext.GetValue(fieldInfoSet.GetValue(dbSet))
.GetType()
.GetProperty(_owner, KeyPropertyBindingFlags)
.GetValue(fieldInfoContext.GetValue(fieldInfoSet.GetValue(dbSet))));
一旦定义了上下文,它就会在upsert函数中使用。 参见示例代码
//for updating the existing records
dbContext.Entry(_exist).State = EntityState.Detached;
dbContext.Entry(entity).State = EntityState.Modified;
//for adding a new seat of records
dbContext.Set<TEntity>().Add(entity);
dbContext.Entry(entity).State = EntityState.Added;
该函数包含简单的验证(本地和远程)。 本地验证可以防止具有相同键的重复记录插入数据库,假设你正确构建了数据库。
如何使用?
假设你有一个EDMX表。
using (var db = new ())
{
//db optional config
db.Configuration.AutoDetectChangesEnabled = false;
db.Configuration.ValidateOnSaveEnabled = false;
using (var dbContext = db.Database.BeginTransaction(
System.Data.IsolationLevel.Serializable)
)
{
try
{
db.<Database Name>.EFUpsert(
x => (your condition),
new <Your Model Class>()
{
//add or updated value here
});
db.SaveChanges();
dbContext.Commit();
}
catch
{
dbContext.Rollback();
}
}
}
很简单,对吧? 你可以试试。
如果你发现任何更正或错误,请告诉我。