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

7

我非常新于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>

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

你需要一个连接字符串。此外,在配置文件中的连接字符串名称是在创建实例时传递给DBContext的名称。 - Darren Wainwright
4个回答

9
在您的<configSections>标签下,您可以放置以下内容。
<connectionStrings>
    <add name="myConn" connectionString="theConnString" providerName="System.Data.SqlClient" />
</connectionStrings>

然后用你所需的值替换原有数值

另外,虽然这可能无法在此情况下帮助到您,但为了避免将来手动执行此操作,EntityFramework NuGet软件包非常好用。只需输入数据库的URL和登录信息,它就会为您在配置文件中创建整个连接字符串,创建您的edmx并允许您导入所需的数据。


1
这个方法可行,但只有在我将“System.Data.EntityClient”更改为“System.Data.SqlClient”之后才能正常运行。在此之前,我一直收到“不支持关键字'server'”的错误提示。无论如何,感谢您的帮助 - 它让我达到了我需要的目标。 - PJW
那很好,抱歉没有注意到那一个。 - Alfie Goodacre

2
这应该可以做到:
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="putYourEntityName" connectionString="putYourConnectionStringHere" providerName="System.Data.EntityClient"/>
    </connectionStrings>
</configuration>

这听起来可能很愚蠢,但实体名称是什么?这是我的上下文的名称,数据库的名称还是类库项目的名称(或其他一些名称)? - PJW
1
@PJW 通常与您的 DbContext 类的名称相同。 - rbr94

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>

...


0

首先需要知道的是,如果您正在使用类库(.dll),并且在 MVC 应用程序中调用此方法(该应用程序具有 web.config),则在 .dll 中创建的实体(.edmx)文件将不会使用 App.config 中的连接字符串。

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


没有web.config文件。 - PJW
我没有看到任何提示表明这是MVC应用程序。通常需要为使用此模型的EDM定义连接字符串。这意味着,如果您在应用程序中使用包含EDM的DLL,则需要将连接字符串放置在应用程序的app-config而不是DLL的app-config中。 - rbr94

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接