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