0

My Code is :

public class EmployeeController : ApiController
    {
        ASITDbEntities db = new ASITDbEntities ( );
        // GET api/employee
        public IEnumerable<Employee> Get ( )
        {
            db.Configuration.ProxyCreationEnabled = false;
            var result=db.Employees.ToList ()
            return result.ToList ( );
        }
}

I want to return employee district name from District table. It returns only district Id.

I tried db.Employee.Include(e=>e.district).Tolist(); But that did not work. Please help me.

4
  • "But did not work" What happened instead? Was there an error message? What are you expecting for the final result? Commented Nov 20, 2018 at 18:29
  • db.Employees.Include(e=>e.district).Tolist(); shouuld do the trick unless there isn't actually a value set in the database for the districtId. Commented Nov 20, 2018 at 18:42
  • I Used It ,var result = db.Employees.Include ( e => e.district ).ToList ( ); it shows this error ,"A specified Include path is not valid. The EntityType 'ASITDbModel.Employee' does not declare a navigation property with the name 'district'." Commented Nov 20, 2018 at 18:50
  • You should show the relevant parts of the class model. If Employee has something like DistrictId it's almost certain that you can add a navigation property District to it. Commented Nov 20, 2018 at 21:02

1 Answer 1

1

Firstly are you using DB first or Code-first. If code first, Does your entities looks like this?

Public class Employee{
  public int EmpId {get;set;}
  public int EmpName {get;set;}

  public int DistrictId {get;set;}
  public District District {get;set;}
}

public class District{
  public int DistrictId {get;set;}

  public List<Employee> Employees {get;set;}
}

It creates 1-1 relationship between Employee and District entities. So, now if you include db.Employee.Include(e=>e.district).Tolist(), should return you the complete District object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.