如何参考另一个模型的列表映射模型

时间:2019-01-28 19:44:44

标签: c# sql-server asp.net-mvc

我有一个MVC项目,尝试在其中使用SQL查询创建一对多关系。我有三种模式。使用第一个模型,第二个模型是部门,第三个模型是文档。目前,我仅从以下代码中获取员工数据。如何映射所有三个表并检索数据。

public class Employee 
{
   public long EmployeeId { get; set; }
   public string Employeename { get; set; }

   public List<Department> Departments { get; set; }
   public List<Document> Documents { get; set; }
}

public class Department 
{
   public long DepartmentId { get; set; }
   public string Departmentname { get; set; }
   public long EmployeeId { get; set; 
}
public class Document 
{
   public long DocumentId { get; set; }
   public sting Documentname { get; set; }
   public long EmployeeId { get; set; }
}

       public Employee(long EmployeeId)
    {
        var sqlStatement = @"select * from Employee where EmployeeId = :EmployeeId";
        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ConnectionString))
        {
            connection.Open();
            using (var sqlCommand = new SqlCommand(sqlStatement, connection))
            {
                sqlCommand.Parameters.Add("@EmployeeId", SqlDbType.VarChar).Value = EmployeeId;
                using (var reader = sqlCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Employeename  = reader["Employeename "].ToString();
}

}

0 个答案:

没有答案