Queries with entity framework

0

I need to make a simple query in a login.

The query would be to see how many employees are registered, if there are no registered employees show a message and then register it.

thanks

public partial class WPF1_Login : MetroWindow
{
    STKDB bdd = new STKDB();
    Funciones fn = new Funciones();

    public int empleados;

    public WPF1_Login()
    {
        InitializeComponent();
    }
    private async void MetroWindow_Loaded(object sender, RoutedEventArgs e)
    {
        if (empleados == 1)


        {
            string msj1 = "No hay empleados registrados";
            Btn_Aux.Content = "Crear Usuario Administrador";
            await fn.Crear_MensajeAsync(Grd_Cont, msj1, 1000);
        }
        else if (empleados == 0)
        {
            string msj1 = "Empleados es 0";
            Btn_Aux.Content = "Crear Usuario Administrador";
            await fn.Crear_MensajeAsync(Grd_Cont, msj1, 1000);
        }
    }
    
asked by positiveSteve 05.12.2018 в 20:10
source

1 answer

0

You should use the context of entity framework to consult the table d employees, something like:

private async void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
    STKDB bdd = new STKDB();
    int empleados = bdd.Empleados.Count();

    if (empleados == 1)
    {
        string msj1 = "No hay empleados registrados";
        Btn_Aux.Content = "Crear Usuario Administrador";
        await fn.Crear_MensajeAsync(Grd_Cont, msj1, 1000);
    }
    else if (empleados == 0)
    {
        string msj1 = "Empleados es 0";
        Btn_Aux.Content = "Crear Usuario Administrador";
        await fn.Crear_MensajeAsync(Grd_Cont, msj1, 1000);
    }
}

The idea is that the context you define local where you are going to use it, then you access the property that maps with the employee table and you perform Count() to know the number of records

I do not know why you evaluate a value == 1 as there are no employees, but hey, that I imagine will depend on your logic

    
answered by 05.12.2018 / 21:05
source