Alex Alex - 1 year ago 294
C# Question

WPF MVVM Light Toolbox with Entity framework c# simple linq

What is the best way to use MVVM Light and Entity framework ?

  1. I was create MVVM Light project.

  2. I was add entity DB to Model folder. (DB.edmx)

  3. I was add to MainWindow.xaml DataGrid and textbox.

  4. What is next step for example to show user name in textbox or all data in DataGrid?
    Where I need add this logic?
    dc.Employees.FirstOrDefault(s => s.EmployeesId == employeesId);

    I need use "Model" or "Modelview" folder to add my logic?

I found some tutorial, but there are too difficult logic 2 time need create GetEmployees and ect.

Answer Source

In ViewModel folder you would have a class EmployeeViewModel. This is what you need to build, and you don't care where that data comes from.

So you can have another class ( EmployeeDataService ) that creates the EmployeeViewModel. This is an intermediary class, that neither belongs in the ViewModel, nor the Model folder. You can place it in a folder like 'DataServices'.

In Model you can have a method that returns an Employee:

    public Employee GetEmployee(){
        return dc.Employees.FirstOrDefault(s => s.EmployeesId == employeesId);

Then, in DataServices\EmployeeDataService.cs you can have:

public EmployeeViewModel GetEmployeeViewModel(id){
     Employee employeeModel = ModelClass.GetEmployee(id);
     EmployeeViewModel employeeVm = ' ..code to build an EmployeeViewModel from employeeModel
     return employeeVm;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download