Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework losing binding to connection string in Visual Studio

I'm having a problem where the Entity Framework edmx file is apparently losing its relationship to the connection string that it was built with.

The designer for my .edmx file was working perfectly - I could do "Update from database" and it would always jump to the wizard step that would allow me to add tables, procs, etc. Now when I click "Update from database", I get the "Choose your data connection" wizard step where it wants me to select a new database - if I go through these steps, it will create a new connection string for me, and regenerate all of the objects as if I was starting from scratch.

The contents of my app.config file are exactly as they were prior to the problem, but I did modify the file, then revert it back the way it was. I'm thinking somehow modifying this file triggers something that causes me to lose the binding, but I can't figure out what that is.

The connection string key in the config file matches the CdmEntityContainer attribute in the EDMX file.

I've even tried killing my codebase and re-checking-out from source control, but that doesn't seem to work. Other people working on the same codebase are having this same problem, so it seems that there's something definitely wrong somewhere in the project. But there were no relevant changes to the .csproj or .sln files - just nothing I can see anywhere that could be causing this.

Any ideas? The only solution I'm seeing right now is to delete and recreate the edmx files from scratch, but I'm definitely hoping to find something better.

EDIT: If I generate a brand new edmx in the project, unrelated to any of the existing ones, it seems to fix the rest. It must "reset" whatever is funky in the project. But this can't be a legitimate solution, so I'm still hoping for something better.

like image 619
Joe Enos Avatar asked Mar 24 '11 00:03

Joe Enos


People also ask

How do I update Entity Framework connection string?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.

How do I find the database connection string in Visual Studio?

7] In Visual Studio go to View -> Server Explorer. 8] In Server Explorer window, Under Data Connections Select your Database. Right Click your Database -> Click Properties. 9] In Properties window you will see your Connection String.


4 Answers

I just had this issue and in my connection string I was missing

application name=EntityFramework in the data Source

like image 136
DavidCharlesSmith1979 Avatar answered Oct 04 '22 09:10

DavidCharlesSmith1979


I just had this and solved it.

The problem was that the edmx was being used as my DAL, with its own project and web config, and the DAL was called by my ASP.NET project (the solution's default project) with ITS own web config.

The configs had been the same for ages, but the ASP.NET project was pointed to another server (ie the connection string changed). Even though the DAL's connection string was still unchanged, valid and the ASP.NET project wasn't involved when you want to "Update model from database" at design time, it must be something to do with the default project clashing with it.

I changed the connstring in the web config of the project containing the edmx to be the same as the default project's one, and it worked again.

like image 28
SteveCav Avatar answered Oct 04 '22 09:10

SteveCav


If you happen to be bouncing between environments (e.g. dev, prod) when this problem popped up and you are running different SQL Server version (e.g. 2005, 2008) in those environments, you can have some problems. The DB version number gets stored somewhere in the .edmx file.

If that is the case, let me know and I can dig up the specifics and respond with them. If not, I don't know what it could be.

Good luck!

EDIT: I realize this probably has nothing to do with the actual problem, but I wanted to provide the specifics I mentioned in case someone else runs across this. The ProviderManifestToken attribute in the Schema element (inside the .edmx) contains the database version number. Note that it actually uses the year (e.g. 2005, 2008) instead of the true DB version number. If you create a model on one version and move it to another version, you'll need to manually edit the file to change this value.

<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="..." Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
        ...
      </Schema>
    </edmx:StorageModels>
  </edmx:Runtime>
</edmx:Edmx>
like image 25
John Laffoon Avatar answered Oct 04 '22 10:10

John Laffoon


Old question but I thought I would post the fix that worked for me for those who still have this problem. In my case, it was simply a password issue. I was working in test, but the connection string was using the password for production. A more helpful response by VS might be to display an "Invalid login or password" type of message since that was actually the problem.

like image 24
dobie-g Avatar answered Oct 04 '22 09:10

dobie-g