mirror of
https://github.com/bitwarden/server.git
synced 2025-04-27 07:42:15 -05:00

* Enable Nullable In Unowned Repos * Update More Tests * Move to One If * Fix Collections * Format * Add Migrations * Move Pragma Annotation * Add Better Assert Message
22 lines
495 B
C#
22 lines
495 B
C#
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;
|
|
}
|
|
}
|