简体   繁体   中英

error occurred when requesting data via Entity Framework

I'm building an N-tier application which has to send JSON data, which is read from SQL Server 2012 through Enity Framework.

When I try to request a collection of users I get an "An error has occurred" page. It works with hardcoded data.

This is my code:

public IEnumerable<User> Get()
{
    IUserManager userManager = new UserManager();
    return userManager.GetUsers();
}


public IEnumerable<User> GetUsers()
{
    return repo.ReadUsers();
}

public IEnumerable<User> ReadUsers()
{
    IEnumerable<User> users = ctx.Users.ToList();
    return users;
}

"ctx" is a reference to a DbContext-object.

EDIT: This works:

public IEnumerable<User> Get()
{
    IList<User> users = new List<User>();
    users.Add(new User() { FirstName = "TestPerson1" });
    users.Add(new User() { FirstName = "TestPerson2" });

    return users;
}

Browser screenshot: http://i.imgur.com/zqG0qe0.png

EDIT: Full error (screenshot): http://i.imgur.com/dt48tRG.png

Thanks in advance.

If your website returns internal error and no call stack you are not seeing the full exception(which makes it kind of hard to exactly point out your problem).
So first of all to get to the actual exception with call stack you have 2 methods.

  • Debug the website : start the website locally or attach your debugger to a locally running website. While stepping through the code the debugger will stop when it hits an exception and you'll see the exception details then.
  • Disable custom errors : IIS wants to protect your internal workings so standard behavior is not to show full exceptions. To disable this behavior edit your web.config and add the xml node under

After you get the actual exception please update your question with call stack & the real internal server error. My guess is that you have a serialization issue, maybe a circular reference of some sort. You're fix would be to either make a simpel viewModel(and not return the entity directly) or add serialization settings(json.net support circular references for example).


As suspected the serialization is giving you a hard time. The cause is the proxy creation used by the lazy loading. You can disable the proxy creation with the following code(make note that this also disables lazy loading).

public IEnumerable<User> ReadUsers()
{
    ctx.Configuration.ProxyCreationEnabled = false;
    IEnumerable<User> users = ctx.Users.ToList();
    return users;
}

if this works you might consider disabling proxy creation during the context initialization.

Try to return plain model, smth like

var logins = ctx.Users.Select(user => new FlatUserModel
                                    {
                                        Id = user.Id,
                                        Name = user.UserName,
                                        Email = user.Email
                                    })
                                    .ToArray();
return logins;

Also look in browser what do you get in response.

As it works with hard coded data there is possibility of lazyloading enabled on context.

If so disable lazy loading in the context or like this so that serialization to JSON does not load the entities from database.

public IEnumerable<User> ReadUsers()
{
    ctx.Configuration.LazyLoadingEnabled = false;
    IEnumerable<User> users = ctx.Users.ToList();
    return users;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM