0

I have a simple table as follows mapped with Entity Framework. I am trying to return just a very basic call of IEnumerable(Of MyTableObject) however MVC API is just bombing out with error code 500. When I try to return a IEnumerable(Of String) and select just a single column it does the same.

Entity Framework Model

I try to return this model with the following code.

' GET api/chatlog
Public Function GetValues() As IEnumerable(Of ChatLog)
    Using mcEntity As New McEntities

        Return mcEntity.ChatLogs.ToList()
    End Using
End Function

Unfortunate for me with the return data. I get nothing?

Does MVC Web API not support passing objects from EF down the pipe or do I manually have to Serialize this into XML?

Fiddler throws "HTTP/1.1 500 Internal Server Error"

5
  • Have you tried returning an array? Commented Oct 18, 2012 at 22:11
  • I just noticed you're calling ToList but stil returning IEnumerable, try changing the return type Commented Oct 18, 2012 at 22:12
  • I had in a couple of tests and used both ToArray() & AsEnumerable() maybe it would have been better to add it to the above. It throws a different exception. "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection." so removed the using statement to no avail. Additionally changing the return type ToList(Of ChatLog) throws the same. Fiddler isn't showing much either :-(
    – Joey Bob
    Commented Oct 18, 2012 at 22:18
  • That objectcontext error sounds like a scoping issue. I've seen that when I've had the wrong lifestyle in castle Windsor. Perhaps you should pursue that further. It might be worth asking another SO question regarding the ObjectContext problem. Commented Oct 18, 2012 at 22:22
  • I checked the code, when the data is inside a "Using" statement. The connection appears to get closed prior to the data being returned by the controller. I rebuilt the same project on a different machine from scratch and it works, which leads me to believe its a configuration issue.
    – Joey Bob
    Commented Oct 19, 2012 at 8:39

1 Answer 1

0

Couple of things,

  1. Can you enable exception details in error messages by doing,

    Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

    and share the exception message and stack trace?

  2. Regarding the using statement, it is generally suggested to dispose your EFContext in the controller and not in the action. ApiController is an IDisposable. You should override the Dispose method of the controller and dispose your DBContext there. Example,

    public CustomersContext _db = new CustomersContext();
    
    public IEnumerable<Customer> Get ()
    {
        return _db.Customers;
    }
    
    protected override Dispose(bool disposing)
    {
        base.Dispose(disposing);
    
        if(disposing)
        {
            _db.Dispose();
        }
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.