I have simple controller:
public IEnumerable<Company> GetAll() {
using (SomeEntities data = new SomeEntities()) {
return data.Companies.ToList<Company>();
}
}
And Company class from ADO.NET Entity model:
namespace Some.Models {
using System;
using System.Collections.Generic;
public partial class Company {
public Company() {
this.Employees = new HashSet<Employee>();
}
public int COMP_ID { get; set; }
public string COMP_NAME { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
}
My problem is that the controller returns JSON with all companies with all employees.
I just want to get list of Companies (like in DB - employees have FK to Companies, not the other way around). I don't want them in list of Companies.
I tried to set getter of Employees in Company model to protected, which works, but then I can't access Emplyees in C# code so I need to create new method in Company model:
public ICollection<Employee> GetEmployees() {
return Employees;
}
And after every Update Model From Database my code is purged and I need to write it again.
This solution doesn't seem proper. What is the proper way to achieve this? Thanks