实体:
[Serializable]
[DataContract]
public class Employee
{
[DataMember]
[Key]
[Required]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string PosName { get; set; }
[DataMember]
public int? Gender { get; set; }
}
接口:
[OperationContract]
IQueryable<Employee> GetEmps(int organzationId);
实现:
public IQueryable<Employee> GetEmps(int organzationId)
{
return _objContext.ExecuteStoreQuery<Employee>("select u.ID,e.Name,P.Name as PosName,e.Gender from sys_user u inner join hr_employee e on u.ID=e.[User] inner join hr_Position p on e.Position=p.ID where u.Forbidden=0 and e.Organization=@orgid", new SqlParameter("@orgid", organzationId)).AsQueryable();
}
运行,没有问题,但是在实体里增加一个byte[]类型的属性,如下:
[Serializable]
[DataContract]
public class Employee
{
[DataMember]
[Key]
[Required]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string PosName { get; set; }
[DataMember]
public int? Gender { get; set; }
[DataMember]
public byte[] Photo { get; set; }
}
运行,报错:服务积极拒绝。但是调试服务端,并不出错。经尝试,将接口改成如下:
[OperationContract]
IList<Employee> GetEmps(int organzationId);
实现改成如下:
public IList<Employee> GetEmps(int organzationId)
{
return _objContext.ExecuteStoreQuery<Employee>("select u.ID,e.Name,P.Name as PosName,e.Gender,e.Photo from sys_user u inner join hr_employee e on u.ID=e.[User] inner join hr_Position p on e.Position=p.ID where u.Forbidden=0 and e.Organization=@orgid", new SqlParameter("@orgid", organzationId)).ToList();
}
运行OK。
分析:应该是执行AsQuery操作是延迟加载数据,而执行ToList操作则立即加载数据。但是为什么在无byte[]类型的属性时就能正常运行,我还没弄明白。