应用程序配置中的实体框架连接字符串

本文关键字:框架 连接 字符串 实体 配置 应用程序 | 更新日期: 2023-09-27 18:14:56

我是实体框架的新手,如果这个问题是基本问题,我很抱歉。

我正在阅读一本"代码优先"的课本,并创建了一个小类库(c#(和一个控制台应用程序。本书指出,当我运行它时,实体框架将自动在SQL server中构建数据库。它没有,我相信的原因是我有一个命名的实例,而不是默认的实例''SQLExpress。课本上显示,在使用默认实例时,我不需要连接字符串,但由于我没有使用默认实例,我猜我确实需要App.Config中的connectionm字符串。我曾尝试在该文件中放入标准连接字符串,但是它不起作用。

以下是我的应用程序配置文件的全部内容

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

然而,什么都没有发生,即没有像以前那样创建数据库。

应用程序配置中的实体框架连接字符串

在您的<configSections>标签下,您可以放置此

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

然后用您需要的替换这些值

此外,尽管在这种情况下它可能对您没有帮助,但为了避免将来不得不手动执行此操作,EntityFramework NuGet包的效果很好,您只需输入数据库的url和登录名,然后它会在配置文件中为您创建整个连接字符串,创建edmx,并允许您导入所选的数据

这应该做到:

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

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

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