I create class Employee:
public class Employees : BaseModel
{
[Key]
public int EmployeeId { get; set; }
// [ohter parameter]
public int? DepartmentId { get; set; }
public virtual Departments Departments { get; set; }
public int? OccupationId { get; set; }
public virtual Occupations Occupations { get; set; }
}
I want to create a view model.
public class Employee_ViewModel
{
public Employee_ViewModel(Employees item)
{
this.EmployeeId = item.EmployeeId;
//[other parameter]
this.DepartmentId = item.DepartmentId;
this.OccupationId = item.OccupationId;
}
public int EmployeeId { get; set; }
//[other parameter]
public string DepartmentName { get; set; }
public string OccupationName { get; set; }
}
my method which returns Employee_ViewModel look like:
List<Employee_ViewModel> item = contex.Employees.Where(w => w.IsActive == true && w.IsVisible == true)
.Include(i => i.Departments)
.Include(i => i.Occupations)
.Select(s => new Employee_ViewModel(s)
{
OccupationName = s.Occupations.Name,
DepartmentName = s.Departments.Name
})
.ToList();
Finally I get error:
Only parameterless constructors and initializers are supported in LINQ to Entities.
Solution form the this post:
Only parameterless constructors and initializers are supported in LINQ to Entities
is working but why my constructor doesn't work?