Files
server/src/Infrastructure.EntityFramework/EfExtensions.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

22 lines
495 B
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
#nullable enable
namespace Bit.Infrastructure.EntityFramework;
public static class EfExtensions
{
public static T AttachToOrGet<T>(this DbContext context, Func<T, bool> predicate, Func<T> factory)
where T : class, new()
{
var match = context.Set<T>().Local.FirstOrDefault(predicate);
if (match == null)
{
match = factory();
context.Attach(match);
}
return match;
}
}