I am currently using Asp.net database context to create a API, and I came into a problem. I have two Database Tables and I want to output some of the data but not all of it
My tables:
Employee Table:
+----+------+---------------+------------+---------------+
| Id | Name | Mail | Phone | Birthday |
+----+------+---------------+------------+---------------+
| 1 | John | John@Mail.com | 987 72 123 | 25-July 2000 |
| 2 | Stan | Stan@Mail.com | 978 21 342 | 10-April 1998 |
| 3 | Kim | Kim@Mail.com | 973 28 199 | 26-March 2001 |
+----+------+---------------+------------+---------------+
Shift Table:
+------------+--------------------+--------------------+--------+
| EmployeeId | Start | End | Active |
+------------+--------------------+--------------------+--------+
| 1 | 10-June 2019 08:00 | 10-June 2019 16:00 | true |
| 2 | 10-June 2019 12:00 | 10-June 2019 18:00 | true |
| 3 | 10-June 2019 16:00 | 11-June 2019 00:00 | true |
| 2 | 11-June 2019 08:00 | 11-June 2019 16:00 | true |
+------------+--------------------+--------------------+--------+
In my tables the Employee Table and Shift Table have a 1-Many relationship between Employee.Id and Shift.Id
Using the controller I have created i have been able to return all of the data, however I want to return a specific data-set (not everything) basically what i want to return is only:
+----+------+--------------------+--------------------+--------+
| Id | Name | Start | End | Active |
+----+------+--------------------+--------------------+--------+
| 1 | John | 10-June 2019 08:00 | 10-June 2019 16:00 | true |
| 2 | Stan | 10-June 2019 12:00 | 10-June 2019 18:00 | true |
| 3 | Kim | 10-June 2019 16:00 | 11-June 2019 00:00 | true |
| 2 | Stan | 11-June 2019 08:00 | 11-June 2019 16:00 | true |
+----+------+--------------------+--------------------+--------+
I have tried the following code in the Controller, but I cant seem to be able to return the Data-set i want
private readonly DatabaseContext context;
public ManageUsersController(DatabaseContext Dbcontext)
{
context = Dbcontext;
}
[HttpGet("Users")]
public List<Employee> GetUsers()
{
var data = context.Employee.Include(c=> c.Schedule).toList();
return data;
}
The models i retrieve the data looks like this
public partial class Employee
{
public int Id { get; set; }
public int PhoneNr { get; set; }
public string Name { get; set; }
public string Mail { get; set; }
public DateTime Birthday { get; set; }
public bool Active { get; set; }
//Relationship
public ICollection<Schedule> Schedule { get; set; }
}
public partial class Schedule
{
public int Id { get; set; } //EmployeeId
public DateTime Start { get; set; }
public DateTime End{ get; set; }
public bool? Active { get; set; }
//Relationship
public Employee Employee { get; set; }
}
lastly my DatabaseContext file looks like this:
public class DatabaseContext: DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<Schedule> Schedule { get; set; }
public DbSet<Employee> Employee { get; set; }
}
Hope my question isn't to long, and thanks in advance for the response