以下のように EF Core を使用して、汎用リポジトリで ADDorUpdate() メソッドを実現したいのですが、どなたか手伝っていただけますか?
public virtual void AddOrUpdate(T entity)
{
#region Argument Validation
if (entity == null)
{
throw new ArgumentNullException("entity");
}
#endregion
DbSet.AddOrUpdate(e => e.Id, entity);
this.DbContext.SaveChanges();
}
ベストアンサー1
使用するだけ
context.Update(entity);
これは、AddOrUpdate
エンティティの PrimaryKey の値に基づいて正確に実行されます (0 は追加、> 0 は更新を意味します)。
public virtual void AddOrUpdate(T entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
this.DbContext.Update(entity);
this.DbContext.SaveChanges();
}