Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I used entity framework 6.1 for the default connection string from wcf service. In which i want to get the details of user when the user logins has taken from the database using EF 6.1.

But i am facing problem not connecting to the database and not getting the data from the database.

What I have tried:

var objUser = (from u in dbContext.Users
               where u.UserName.Equals(user.UserName)
               select u).FirstOrDefault();


From this i am not getting the data from the database saying the data has not been connected.
Here dbContext is an object instance for the database entity like
public partial class projectentity : DbContext
{
}

this class is from edmx file and projectentity object dbContext called at some other class outside the project but it is in the same solution....

Now the error point of view i am getting like this...
'dbContext.Users.Local' threw an exception of type 'System.InvalidOperationException'

Can anyone explain me this point. Thanks in advance
Posted
Updated 18-Sep-17 2:53am
Comments
TheSniper105 18-Sep-17 1:31am    
did you check your connection string in config file ?
in server explorer does that connection point to the right database ?
Member 8583441 18-Sep-17 6:46am    
Sorry for the late reply.... Yeah i checked it...
It is working fine each & every step but the error is at that line...
TheSniper105 19-Sep-17 0:56am    
this error can be also happens if you have a wrong entities configuration or mapping! I think something mess between your relationships. is the database at this point exists or it is should be created at this query?
Member 8583441 19-Sep-17 2:43am    
I think @Vaidhya.P already explained me the step by step procedure to check the connection string in the web.config file as you explained me last day.

I am really sorry for the late reply now it is working fine as suggested by you and @Vaidhya.P as well.

So i will accept the answer for @Vaidhya.P because i am having acceptance from her..... You both are right the acceptance will also given to you but i can't because we are in comments section..

So, Please accept the solution from her.... Thanks in advance
TheSniper105 19-Sep-17 3:31am    
never mind dear you are welcome

1 solution

The error you are getting in the DBContext has nothing to do with the code there should be some error in the connection string.

To Solve this problem:

Step 1: Check the connection string in the web.config file --> check the DB context name is correctly specified as in the C# DB context entity name.

Step 2: Go to the App.Config and if you have multiple entries of the connection strings keep the latest one and delete the other connections.

Step 3: Check if the connection string in the App.Config and Web.Config are pointing to the rite Database.

If the above steps dint work,

Try deleting and adding the edmx file again. That should work.

Thanks in advance. :)
 
Share this answer
 
Comments
Member 8583441 19-Sep-17 2:44am    
Thank you nice explaination

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900