Home > Mobile >  How to verify connection string in App.config?
How to verify connection string in App.config?

Time:11-23

I am brand new to Entity Framework and I am attempting to set up this tutorial:

https://github.com/entityframeworktutorial/EF6-DBFirst-Demo

In the readme, it asks me to first open the project in Visual Studio 2017, I used 2019 because that's what is installed on my system so hopefully this is not an issue.

Second, it asks to "Open MS SQL Server 2012 and attach SchoolDB.mdf (included in EF6DBFirstDemo folder)" this took me a few days to figure out because I had several versions of MSSQL server 2012 installed so i removed them all and installed a single version.

Also, I placed the folder with all the files into my desktop under my current user nested a bit down (next to all the other applications I am managing). Unfortunately, by doing this, I could not navigate to the .mdf file in MSSQL Management Studio so I had to copy this file over into a location that was visible to the management studio which turned out to be 'C:/Program Files/Microsoft SQL Server/MSSQL11.SQLEXPRESS/MSSQL/DATA', Anyhow I eventually got the database installed and I can see the tables and such now.

The third step says to "Verify the connection string in App.config in Visual Studio and make sure that it is pointing to your local DB server." Having absolutely no idea how this is done, I crossed my fingers and clicked the go icon in visual studio. The application of-course immediately blows up.

My question is, what exactly do I need to change in the App.config file to make it point at my particular local database. Here is a copy of what that file has currently:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>

  <connectionStrings>
    <add name="SchoolDBEntities"
         connectionString="metadata=res://*/School.csdl|
                                    res://*/School.ssdl|
                                    res://*/School.msl;
                           provider=System.Data.SqlClient;
                           provider connection string=
                               &quot;data source=.;
                                    initial catalog=SchoolDB;
                                    integrated security=True;
                                    MultipleActiveResultSets=True;
                                    App=EntityFramework&quot;"
        providerName="System.Data.EntityClient" />
  </connectionStrings>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

</configuration>

I have a feeling the reason it's not working is because the code exists within my users directory and the database it is trying to reach was installed from C/Program files/whatever, but this is just a hunch.

Any ideas?

CodePudding user response:

For data source specify the same server name you used when you connected via SSMS. Since you are using Express edition (as suggested by the file path), the local named instance specification should probably be data source=.\SQLEXPRESS.

  • Related