17 lines
574 B
C#
17 lines
574 B
C#
|
using System;
|
|||
|
using System.Linq;
|
|||
|
using System.Linq.Expressions;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|||
|
|
|||
|
namespace Geekbot.net.Lib.Extensions
|
|||
|
{
|
|||
|
public static class DbSetExtensions
|
|||
|
{
|
|||
|
public static EntityEntry<T> AddIfNotExists<T>(this DbSet<T> dbSet, T entity, Expression<Func<T, bool>> predicate = null) where T : class, new()
|
|||
|
{
|
|||
|
var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any();
|
|||
|
return !exists ? dbSet.Add(entity) : null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|