Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.Net Web Api,从外部选项卡“代码优先数据库”返回或输入值_Asp.net_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

Asp.Net Web Api,从外部选项卡“代码优先数据库”返回或输入值

Asp.Net Web Api,从外部选项卡“代码优先数据库”返回或输入值,asp.net,asp.net-mvc,asp.net-web-api,Asp.net,Asp.net Mvc,Asp.net Web Api,我正在努力回报单个员工,包括来自国外的价值 这是我的员工模型课 public class Employee { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int EmployeeId { get; set; } [Required] [MaxLength(100)] public string Emp

我正在努力回报单个员工,包括来自国外的价值

这是我的员工模型课

public class Employee
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EmployeeId { get; set; }

        [Required]
        [MaxLength(100)]
        public string EmployeeName { get; set; }

        [ForeignKey("Department")]
        public int DepartmentId { get; set; }
        public Department Department { get; set; }

        [ForeignKey("Gender")]
        public int GenderId { get; set; }
        public Gender Gender { get; set; }


    }
系与性别示范班

public class Department
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int DepartmentId { get; set; }
        [Required]
        [MaxLength(50)]
        public string DepartmentName { get; set; }
    }

public class Gender
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int GenderId { get; set; }
        [Required]
        [MaxLength(50)]
        public string GenderName { get; set; }
    }
使用下面的代码,我可以在返回时返回inklusive departmentname和gendername

all employees
// GET: api/Employees
        public IQueryable<Employee> GetEmployees()
        {
            return db.Employees.Include(c => c.Department).Include(g => g.Gender); ;
        }
但我不知道当我想退回一个员工时怎么退回。。。我正在将部门和性别设置为空。 这是我的密码。。我不知道如何在这名员工中包括部门名称和性别名称。我只得到部门ID和性别ID,但没有部门名称和性别名称

[ResponseType(typeof(Employee))]
        public async Task<IHttpActionResult> GetEmployee(int id)
        {
            Employee employee = await db.Employees.FindAsync(id);
            if (employee == null)
            {
                return NotFound();
            }

            return Ok(employee);
        }
提前谢谢你

试试看

return wait db.Employees.Includec=>c.Department.Includeg=>g.Gender.SingleAsynce=>e.EmployeeId==id