我正在使用Web Api,我將不得不創建用於在應用程序的UI上顯示數據的數據傳輸對象。如何將一類類型的集合轉換爲實體框架中的另一類類型集合
我與代碼第一種方法在這裏工作是我的域類
public class Employee
{
[Key]
public int BusinessEntityId { get; set; }
[Required]
[MaxLength(50)]
public string JobTitle { get; set; }
[Required]
[DataType(DataType.DateTime)]
public DateTime BirthDate { get; set; }
[Required]
[MaxLength(1)]
public string MaritalStatus { get; set; }
[Required]
[MaxLength(1)]
public string Gender { get; set; }
[Required]
[DataType(DataType.DateTime)]
public DateTime HireDate { get; set; }
[Required]
public Boolean SalariedFlag { get; set; }
public ICollection<EmployeePayHistory> PayHistories { get; set; }
}
這裏是我的數據傳輸對象(DTO)類
public class EmployeePayHistoryListDTO
{
public int Id { get; set; }
public DateTime RateChangeDate { get; set; }
public Decimal Rate { get; set; }
public Int16 PayFrequency { get; set; }
public String JobTitle { get; set; }
public String Gendre { get; set; }
}
現在由於PayHistories是集合在我的領域類的我我正在做的是我正在創建一個新班級 其中有收集我的DTO班級類型EmployeePayHistoryListDTO
public class EmployeeRelatedCollections
{
public ICollection<EmployeePayHistoryListDTO> PayHistories { get; set; }
}
所以從我的倉庫,我正確地通過以下EF聲明
_context.Employees.Include("PayHistories")
.Include("PayHistories")
.Single(e=>e.BusinessEntityId==id);
但我在哪裏將我的Employee類(區域類)的集合,我DTO類型的集合有我在這裏得到錯誤的獲取數據代碼
PayHistories = (from ph in employee.PayHistories
select new EmployeePayHistoryListDTO
{
Id = ph.BusinessEntityId,
RateChangeDate = ph.RateChangeDate,
Rate = ph.Rate,
PayFrequency = ph.PayFrequency,
JobTitle = ph.Employee.JobTitle,
Gendre = ph.Employee.Gender
}).ToList();
I am getting following exception below is summary
System.NullReferenceException ,
Additional Information: Object reference Not set to an instance of an object.
Troubleshooting tips
1. Check to determine if the object is null before calling the method,
2. Use new keyword to create an object instance.
我想你錯過了你的錯誤貼:P – Kritner 2014-09-03 13:56:20
是的非常感謝你,我已經編輯它現在再次PLZ再次看到。 – ProgrammingNinja 2014-09-03 13:59:25
其中是空引用發生? – Kritner 2014-09-03 14:03:46