2014-09-03 96 views
2

我正在使用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 

Execption getting

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. 
+1

我想你錯過了你的錯誤貼:P – Kritner 2014-09-03 13:56:20

+0

是的非常感謝你,我已經編輯它現在再次PLZ再次看到。 – ProgrammingNinja 2014-09-03 13:59:25

+0

其中是空引用發生? – Kritner 2014-09-03 14:03:46

回答

1

來源:

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(); 

你能把它:

PayHistories = (from ph in employee.PayHistories 
         select new EmployeePayHistoryListDTO 
         { 
          Id = ph.BusinessEntityId, 
          RateChangeDate = ph.RateChangeDate, 
          Rate = ph.Rate, 
          PayFrequency = ph.PayFrequency 

         }).ToList(); 

,看看是否異常仍發生?

它看起來像基於該查詢你會:

Employee -> PaymentHistory -> Employee. 

在你的聲明:

_context.Employees.Include("PayHistories") 
           .Include("PayHistories")         
           .Single(e=>e.BusinessEntityId==id); 

它看起來並不像你將包括在頂部的額外員工的對象PayHistories(你有沒有將它包含兩次?)。我相信你也可以使用LINQ語句來獲得更強類型包括像

.Include(i => i.PayHistories) 

希望這會幫助你!

+0

如果我按照你的建議。包括(i => i.PayHistories)我得到以下「不能轉換類型字符串的lamda表達式,因爲它不是委託類型」 – ProgrammingNinja 2014-09-03 14:14:35

+1

顯然,Include是一種擴展方法,您需要到「使用System.Data.Entity」來使用它。 PITA我知道,我總是忘記它的名字空間,並且你不能右鍵點擊解析。 http://stackoverflow.com/questions/4544756/using-include-in-entity-framework-4-with-lambda-expressions – Kritner 2014-09-03 14:27:02

+0

在這裏,你是正確的100%非常感謝你,但問題仍然存在@Kritner。 – ProgrammingNinja 2014-09-03 14:37:11

0

確保employee.PayH istories不包含空項,或檢查它的查詢中:

PayHistories = (from ph in employee.PayHistories where null != ph 
etc. . . 

而且,你指的是對「PH」(員工),也可以爲null一個可能延遲加載/未初始化的屬性。

+0

如果員工爲空,則仍會拋出異常。 – 2014-09-03 14:06:22

+0

這是正確的 - 我會將奇怪的SLQy linq代碼轉換爲擴展方法調用,以便在錯誤檢查中獲得更多的靈活性。 – t0yk4t 2014-09-03 14:07:43

3

看起來您沒有初始化您的員工對象。當空引用異常發生時,您應該可以通過將鼠標懸停在其上並檢查它是否爲空來檢查員工對象的值。當您嘗試訪問空對象(在本例中爲PayHistories)上的字段時,會出現null引用異常。

看是否有此代碼避免了異常:

if(employee!=null){ 
    if(employee.PayHistories.Any()){ 

     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(); 
    } 
} 
0

這是我如何解決我的問題。我有多個引用那些沒有正確加載。

有兩種方法

方法1.

的包括擴展方法過載它接受一個Expression<Func<T,U>>

_context.Employees.Include("PayHistories.furtherreferencename").Include("PayHistories.furtherReference"); 

方法2.

使用強類型包括方法。

_context.Employees.Include(i=>i.PayHistories.select(e=>e.FurtherReference)).Include(i=>i.PayHistories.select(e=>e.FurtherReference));

相關問題