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

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

问题描述

我对Entity Framework非常新,所以如果这个问题是基本的,那么道歉。



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



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

  <?xml version =1.0encoding =utf-8?> 
< configuration>
< configSections>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< startup>
< supportedRuntime version =v4.0sku =。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.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>
< / entityFramework>
< / configuration>

有人可以告诉你连接字符串在哪里,或者告诉我,如果我这样做是错误的



更新



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

 <?xml version =1.0encoding =utf-8?> 
< configuration>
< configSections>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< connectionStrings>
< add name =ModelconnectionString =Server = OFFICE-ACER\SQLE; Database = BreakAway; Trusted_Connection = True; providerName =System.Data.EntityClient/>
< / connectionStrings>
< startup>
< supportedRuntime version =v4.0sku =。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.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>
< / entityFramework>
< / configuration>

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

解决方案

< configSections> 标签下,您可以将此

 < connectionStrings> 
< add name =myConnconnectionString =theConnStringproviderName =System.Data.SqlClient/>
< / connectionStrings>

然后用您需要的值替换值



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


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

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.

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.

UPDATE

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

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天全站免登陆