Nau*_*san 8 c# generics repository-pattern entity-framework-core asp.net-core
我想使用如下所示的 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();
}
Run Code Online (Sandbox Code Playgroud)
Arm*_*our 17
只需使用
context.Update(entity);
Run Code Online (Sandbox Code Playgroud)
它完全AddOrUpdate基于实体 PrimaryKey 的值(0 表示添加,> 0 表示更新):
public virtual void AddOrUpdate(T entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
this.DbContext.Update(entity);
this.DbContext.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)