我已经使用了Find(id)与实体框架5.但是集合扩展方法,很多的例子我看到的使用Where(s => s.Id == 1),而我加入FirstOrDefault()来获取对象,而不是一个集合.这是一种风格差异,还是有明显的.Where()偏好的功能原因?
.net linq linq-to-entities entity-framework entity-framework-5
调用下面显示的方法时,Request始终为null.我有一些简单的方法从MVC4 app中的控制器返回JSON数据,控制器使用ApiController作为基类.我的目录函数的代码如下:
public HttpResponseMessage GetDirectory() {
try {
var dir = r.GetDirectory();
if (dir == null) {
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}
var response = Request.CreateResponse(HttpStatusCode.OK, dir, "application/json");
response.Headers.Location = new Uri(Request.RequestUri, "directory");
return response;
} catch (Exception ex) {
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
调用此方法时,从r.GetDirectory()正确加载'dir'.但请求为空.所以Request.CreateResponse()自然会失败,等等.我正在寻找Request为null的原因,或者为了允许返回仍为HttpResponseMessage的重写.
这被称为(在我的单元测试项目中):
var ctrl = new DirectoryController();
var httpDir = ctrl.GetDirectory();
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.