1

My Controller code

   ParaEntities db = new ParaEntities();
    public List<Client> GetAllClients()
    {
        return db.Client.ToList();
    }

Please click this link to see the error message

It is weird that when I am first time to click the button to get all client information then it responses 500. In the second time, I click the button to get all client, which is success.

2
  • its seems a connection string problem.have you retrieved any other records using this context? Commented Jun 8, 2016 at 6:17
  • Yes, I am using this context many times. As I said, it does not work at first time. After first time it works successfully. I think it is some problems in EF issue, such as Lazy Loading or connection string problem like what you said Commented Jun 8, 2016 at 22:02

3 Answers 3

1

You should assign variable and display the data in View. Please change the syntax as i write below.

ParaEntities db = new ParaEntities();
public List<Client> GetAllClients()
{
    var getData= db.Client.ToList();
       if(getData==null)
           {
               return null;
            }
    return getData;
}
Sign up to request clarification or add additional context in comments.

3 Comments

The code is using in WebAPI, so not need to return View. And I have tried your code. some issue occurred
But the thing is if Null data return from your entity then it cause an error. so i write to store data in variable.
Yes. I have tried your code. it does not work. As I said, it does not work at first time. After first time it works successfully. I think it is some problems in EF issue.
0

This error points to a connection problem rather then code issue. Check that the connectionstring is valid and that the user specified in the connectionstring has access to the database. If you're running the application on IIS then make sure that the applicationpool user has access to the database. Here is another SO issue were they solved this error.

If you want to store the db context as a local variable in your controller class then I suggest you to instantiate it inside of the controllers constructor. Then you make sure that every time a instance of the controller is created then a new db context is created as well. Lets say your controller namned ClientController

private ParaEntities db;
public ClientController()
{
    this.db = new ParaEntities();
}

public List<Client> GetAllClients()
{
   return db.Client.ToList();
}

Another approach is to wrap your db context in a using statment inside of your method. In that case you make sure that the method is using a fresh context when being called upon and that the context is being disposed when the operation is completed.

public List<Client> GetAllClients()
{
    using(ParaEntities db = new ParaEntities())
    {
       return db.Client.ToList();
    }
}

PS: both examples violates the dependency inversion principle (hard coupling to the db context) but thats for another day

12 Comments

Hi Marcus, the case is for WebAPI, so I can not dispose db context in this case. However, I have tried your code. It caused another issue. BTW: As I said, it does not work at first time. After first time it works successfully.
@GaoxinHuang by the example you have showed, you can dispose it. What error message did you get when you stored it in the method? Either way I've updated the answer with a new example on how to create your local db context in the controller.
I change current database to a new database server. It works successfully. Is it possible for the issue is form the stability of database server?
Ok. The initial error message indicates that there was an issue with the connection to the database. It could be, as you point out, the database server but as it works for you on the second call it hard to tell. Have you tryed to instantiate the db context in the controller constructor?
It is so weird that I try to connect with database by SSMS. same issue happened, when I firstly connect it and it is failure. Then, I tried more time to connect with database. which it is successfully.
|
0

Please try this

public List<Client> GetAllClients() { ParaEntities db = new ParaEntities(); return db.Client.ToList(); }

3 Comments

Yes probably its an issue in entity framework. I think only after first button click those dbcontext or something like initialization is happening, thats why on second click that values from db are loaded. please check it when the dbcontext is initalized and where
Please put "var data = db.Client.ToList();" before return and check whether "data" is empty on first click
I change current database to a new database server. It works successfully. Is it possible for the issue is form the stability of database server?

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.