我正在使用Web Api,我必须创建数据传输对象,以便在应用程序的UI上显示数据。
我正在使用Code First方法,这是我的Domain类
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是我的Domain类中的集合,我正在做的是创建一个新类
其中包含我的DTO类类型EmployeePayHistoryListDTO
public class EmployeeRelatedCollections
{
public ICollection<EmployeePayHistoryListDTO> PayHistories { get; set; }
}
因此,从我的存储库中,我通过以下EF语句正确获取数据
_context.Employees.Include("PayHistories")
.Include("PayHistories")
.Single(e=>e.BusinessEntityId==id);
但是我将我的Employee类(Domain Class)的集合转换为我的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.
答案 0 :(得分: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();
}
}
答案 1 :(得分: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)
希望这会对你有帮助!
答案 2 :(得分:0)
确保employee.PayHistories不包含空条目,或在查询中检查:
PayHistories = (from ph in employee.PayHistories where null != ph
etc. . .
此外,您在&#34; ph&#34;中指的是可能是懒惰的/未初始化的属性。 (员工),也可能为空。
答案 3 :(得分:0)
以下是我如何解决我的问题。我有多个参考文献没有正确加载。
有两种方法
方法1。
Include扩展方法重载,需要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))
;