If I have multiple dbSets within my dbContext, for example:
- public DbSet Persons { get; set; }
- public DbSet Companies { get; set; }
- public DbSet<...>
How could I than create a generic function to check if the value of a dynamically passed field already occurs?
Function in pseudo code:
bool IsFieldValueUnique<T>(field, fieldValue)
{
return DbSet<T>.Any(x => x.field == fieldValue)
}
And function calls could then be something like:
var result1 = IsFieldValueUnique<Person>(Person.Email, somePersonObject.Email)
var result2 = IsFieldValueUnique<Company>(Company.Identification, someCompanyObject.Identification)
When searching around, I find approaches like using '<Func<T, bool>>', but can't connect the dots together.