App.Config中的实体框架连接字符串

时间:2016-10-26 12:20:44

标签: c# entity-framework app-config

我是Entity Framework的新手,所以如果这个问题是基本的话,请道歉。

我正在编写Code First教科书,并创建了一个小类库(c#)和一个控制台应用程序。教科书表明,当我运行它时,Entity Framework将自动在SQL Server中构建数据库。它没有,我相信的原因是因为我有一个命名实例而不是默认实例\ SQLExpress。教科书表明我在使用默认实例时不需要连接字符串,但由于我没有使用默认实例,我猜我在App.Config中需要一个连接字符串。我已经尝试在此文件中放置一个标准连接字符串,但它不起作用。

以下是我的App.Config文件的全部内容

<?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>

有人可以告知连接字符串的去向,或者告诉我是否以错误的方式处理这个问题。

更新

感谢到目前为止收到的帮助,我的App.Config文件现在看起来如下 -

<?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>

但是,没有任何事情发生,即没有像以前那样创建数据库。

4 个答案:

答案 0 :(得分:4)

<configSections>标记下,您可以放置​​此

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

然后将值替换为您需要的值

此外,尽管在这种情况下它可能无法帮助您,但为了避免将来手动执行此操作,EntityFramework NuGet包可以创建奇迹,您只需输入数据库的URL和您的登录信息 - 然后它就会创建整个您的配置文件中的连接字符串,创建您的edmx并允许您导入您选择的数据

答案 1 :(得分:2)

这应该这样做:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="putYourEntityName" connectionString="putYourConnectionStringHere" providerName="System.Data.EntityClient"/>
    </connectionStrings>
</configuration>

答案 2 :(得分:1)

<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>

...

答案 3 :(得分:0)

你需要知道的第一件事。如果您正在使用类库(.dll),则在.dll中创建的实体(.edmx)文件,并且您正在从MVC应用程序(具有web.config)调用此方法。永远不会使用App.config内部的连接字符串。

因此,您可以在Web.Config(MVC项目)上映射Connection字符串,甚至可以从App.Config中删除。它将始终使用web.config。

相关问题