How to return IEnumerable DbSet in web api get?

"user" classes, automatically generated using the entity structure model, the first desinger.

public partial class user { public user() { this.distributorDevice = new HashSet<distributorDevice>(); this.managerDevice = new HashSet<managerDevice>(); this.logUserLogin = new HashSet<logUserLogin>(); this.logUserOperates = new HashSet<logUserOperates>(); this.clientApp = new HashSet<clientApp>(); } public int Id { get; set; } public string name { get; set; } public byte[] password { get; set; } public bool isActive { get; set; } public System.DateTime RegisterTime { get; set; } public int permissionId { get; set; } public System.DateTime lastLoginTime { get; set; } public virtual permission permission { get; set; } public virtual ICollection<distributorDevice> distributorDevice { get; set; } public virtual ICollection<managerDevice> managerDevice { get; set; } public virtual ICollection<logUserLogin> logUserLogin { get; set; } public virtual ICollection<logUserOperates> logUserOperates { get; set; } public virtual ICollection<clientApp> clientApp { get; set; } public virtual customerDevice customerDevice { get; set; } } 

If I wrote like this:

  public class UserController : ApiController { public IEnumerable<user> Get() { List<user> users = new List<user>(); return user; } } 

When I visit "localhost: 3700 / api / user" in explorer, it works.

 <ArrayOfuser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/UpdateServer.Model"/> 

If I wrote like this:

  public IEnumerable<user> Get() { using (var mydb = new ModelContainer()) { return mydb.userSet.AsEnumerable(); } } 

When I visit "localhost: 3700 / api / user" in explorer, I get an error message:

 103 (net::ERR_CONNECTION_ABORTED) 

Follow the tips, I should use ToList () instead of asEnumerable, I change the code as follows

 using (var mydb = new ModelContainer()) { var users = mydb.userSet.ToList(); return users; } 

but still the same error appears in Explorer

 103 (net::ERR_CONNECTION_ABORTED) 

and I add interrupt to

 return users; 

find that the users are already receiving data from the database, but return incorrectly to the client.

I do more tests, add a user with the name "Admin", if I use the code below, the user is not found, and he answers the client correctly.

 var users = mydb.userSet.Where(p=>p.permission.name == "Administrator").ToList(); return users; 

if I write like this

 var users = mydb.userSet.Where(p=>p.permission.name == "Admin").ToList(); return users; 

he was unable to return to the client, report an error

 103 (net::ERR_CONNECTION_ABORTED) 
+4
source share
1 answer

EF uses lazy loading. This means that EF will not actually invoke the database until you try to use the results.

To get around this, you can use ToList () . Unlike AsEnumerable, ToList will immediately execute the request.

Another option is not to delete the context. Although this may seem wrong, you can actually leave the db context alive and let the Garbage collector get it when you no longer use any of the returned objects of the user.

+2
source

All Articles