简体   繁体   中英

Entity Framework Connection String in App.Config

I am very new to Entity Framework so apologies if this question is basic.

I am working through a Code First text book, and have created a small class library (c#) and a console app. The text book indicates that when I run it Entity Framework will build the database in SQL server automatically. It doesn't and the reason I believe is because I have a named instance rather than the default instance \\SQLExpress. The text book indicated I did not need a connection string when using the default instance, but since I am not using a default instance I am guessing I do need a connectionm string in App.Config. I have tried putting a standard connection string in this file but it does not work.

Here are the entire contents of my App.Config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Could someone please advise where the connection string goes, or tell me if I am going about this the wrong way.

UPDATE

Thanks to the help received so far, my App.Config file now looks as follows -

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <configSections>    
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="Model" connectionString="Server=OFFICE-ACER\SQLE;Database=BreakAway;Trusted_Connection=True;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

However, nothing happens ie no database is created as before.

Under your <configSections> tag, you can place this

<connectionStrings>
    <add name="myConn" connectionString="theConnString" providerName="System.Data.SqlClient" />
</connectionStrings>

And then replace the values with what you need

Also although it may not help you in this situation, to avoid having to manually do this in future, the EntityFramework NuGet package works wonders, you just input the database's url and your login - then it creates the entire connection string for you in your config file, creates your edmx and allows you to import the data of your choice

This should do it:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="putYourEntityName" connectionString="putYourConnectionStringHere" providerName="System.Data.EntityClient"/>
    </connectionStrings>
</configuration>
<configuration>
  <configSections>
      ... 
  </configSections>
  <connectionStrings>
    <add name="Name" connectionString="Data Source=servername; Initial Catalog = yourDbName ; Intgrated Security ="According to your requirement" providerName="System.Data.SqlClient" />

  </connectionStrings>

...

First thing that You need to know. If You are using a Class Library (.dll), the Entity (.edmx) file created inside the .dll, and You are invoking this Method, from an MVC Application (That have a web.config). The connection string inside of the App.config will never be used.

So You can have the Connection string mapped on the Web.Config (MVC Project), and even delete from the App.Config. It will always use the web.config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM