mirror of
https://github.com/bitwarden/server.git
synced 2026-02-10 10:55:10 +08:00
20 lines
477 B
C#
20 lines
477 B
C#
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
}
|