c# - App.Config 中的 Entity Framework 连接字符串

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

但是,没有任何反应,即没有像以前那样创建数据库。

最佳答案

在你的<configSections>下标签,你可以把这个

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

然后用你需要的替换值

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

关于c# - App.Config 中的 Entity Framework 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40262151/

相关文章:

c# - 委托(delegate)给实例方法不能有 null 'this'

c# - 从具体化 'System.Guid' 类型到 'System.String' 类型的指定转换无效

.net - 保护配置问题中的密码

cakephp - CakePHP如何处理配置文件

c# - ASP.NET MVC 和身份 - 重置密码不起作用

c# - 使用枚举或字典?

c# - 是否使用存储库模式?需要 ORM? MVC 网络 API REST

c# - 如何使用 Entity Framework Core 加载相关实体

.net - 如何将 app.config 文件加载到 DLL 中

c# - 使 Canvas 跟随相机