Improve migration Script

This commit is contained in:
runebaas 2018-05-13 14:14:50 +02:00
parent 32ae82ca8d
commit 3fa4115502
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
4 changed files with 52 additions and 14 deletions

View file

@ -0,0 +1,17 @@
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;
}
}
}