App.Config 中的实体框架连接字符串 [英] Entity Framework Connection String in App.Config

查看:36
本文介绍了App.Config 中的实体框架连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对实体框架很陌生,所以如果这个问题很基本,我深表歉意.

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

我正在阅读 Code First 教科书,并创建了一个小型类库 (c#) 和一个控制台应用程序.教科书指出,当我运行它时,实体框架将自动在 SQL 服务器中构建数据库.它没有,我相信的原因是因为我有一个命名实例而不是默认实例 \SQLExpress.教科书指出我在使用默认实例时不需要连接字符串,但由于我没有使用默认实例,我猜我在 App.Config 中确实需要一个连接字符串.我试过在这个文件中放置一个标准的连接字符串,但它不起作用.

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.

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

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.

更新

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

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 i.e. 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

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

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

这篇关于App.Config 中的实体框架连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆