我使用Entity Framework 6.0.0.0
,方法是Database First
。 我有三个表Client
,Account
,Doc
。一个Client
有许多Docs
和Accounts
。关系是一对多。 Client
表:为什么Entity Framewok方法“.AsEnumerable()”不返回任何内容?
public partial class Client
{
public Client()
{
this.Account = new HashSet<Account>();
this.Doc = new HashSet<Doc>();
}
public int ClientId { get; set; }
public string Name { get; set; }
public virtual ICollection<Account> Account { get; set; }
public virtual ICollection<Doc> Doc { get; set; }
}
AngularJs
代码片段从WebApi
取数据:
angular.module("app", []).controller("searchController", function ($scope, $http) {
//Used to get and display the data
$http.get('/api/Search/').success(function (data) {
debugger;
$scope.searchs = data;
$scope.loading = false;
})
.error(function() {
debugger;
$scope.error = "An Error has occured while loading posts!";
$scope.loading = false;
});
}
方法Web API
Get
取数据(它没有返回值没有客户。):
[HttpGet]
public IEnumerable<Client> Get()
{
private ClientDBEntities db = new ClientDBEntities();
retutn db.Client.AsEnumerable();// it returns NOTHING. There is no clients
}
然而,$http.get('/api/Search/').success(function (data)
调用,如果我改变这种方法返回IEnumerable<string>
:
[HttpGet]
public IEnumerable<Client> Get()
{
List<string> list = new List<string>(){"1", "2", "3"};
return list;
}
我的问题是为什么db.Client.AsEnumerable()
返回什么?我试图将此代码更改为:
retutn db.Client.ToList();// it returns all necessary data
然而,AngularJS方法调用$http.get('/api/Search/').error(...)
任何帮助将不胜感激。
你得到的错误是什么? –
@AmitKumarGhosh没有错误,只是它的工作原理都可以。我附上了我的观察窗口,以查看我在每个查询中得到的结果。谢谢。 – StepUp
在我看来你正试图返回数据库对象与延迟加载。当反序列化为json时,如果有循环引用,这可能会导致内存不足异常。 –