RSCG_DBContext–usage–part 3 of 3

Install the Nuget package https://www.nuget.org/packages/RSCG_DBContext/
Then decorate your context class with GenerateDbContextExists and make it partial

[RSCG_DBContext.GenerateDbContextExists]
public partial class EmpContext : DbContext
{
    public DbSet Employees { get; set; }
    public DbSet Departments { get; set; }
}

The generator emits a partial class with one Exists_() method per DbSet property, plus an Exists_AllDBSets() helper that checks them all:

// 
namespace SampleDatabase
{
partial class EmpContext
{
    public bool Exists_Department()
    {
        try
        {
            _ = global::System.Linq.Queryable.LongCount(this.Department);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool Exists_Employee()
    {
        try
        {
            _ = global::System.Linq.Queryable.LongCount(this.Employee);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public IEnumerable Problem_DBSets()    {
        if(!Exists_Department()) yield return "Department";
        if(!Exists_Employee()) yield return "Employee";
    }
    public bool Exists_AllDBSets()    {
        try
        {
        if(!Exists_Department()) return false;
        if(!Exists_Employee()) return false;
        }
        catch
        {
            return false;
        }
return true;
    }

}
}

Code at https://github.com/ignatandrei/RSCG_DBContext/

NuGet package: RSCG_DBContext | GitHub repository


Posted

in

,

by

Tags: