[英]Entity Framework Connection String in App.Config
I am very new to Entity Framework so apologies if this question is basic. 我是Entity Framework的新手,如果这个问题是基本的,那么道歉。
I am working through a Code First text book, and have created a small class library (c#) and a console app. 我正在编写Code First教科书,并创建了一个小类库(c#)和一个控制台应用程序。 The text book indicates that when I run it Entity Framework will build the database in SQL server automatically.
教科书表明,当我运行它时,Entity Framework将自动在SQL Server中构建数据库。 It doesn't and the reason I believe is because I have a named instance rather than the default instance \\SQLExpress.
它没有,我相信的原因是因为我有一个命名实例而不是默认实例\\ 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.
教科书表明我在使用默认实例时不需要连接字符串,但由于我没有使用默认实例,我猜我在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 以下是我的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>
Could someone please advise where the connection string goes, or tell me if I am going about this the wrong way. 有人可以告诉我连接字符串的位置,或者告诉我是否以错误的方式处理这个问题。
UPDATE UPDATE
Thanks to the help received so far, my App.Config file now looks as follows - 感谢到目前为止收到的帮助,我的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>
However, nothing happens ie no database is created as before. 但是,没有任何反应,即没有像以前那样创建数据库。
Under your <configSections>
tag, you can place this 在
<configSections>
标记下,您可以放置它
<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 虽然它在这种情况下可能没有帮助,但为了避免将来手动执行此操作,EntityFramework NuGet包可以创建奇迹,您只需输入数据库的URL和登录 - 然后它会在您的配置中为您创建整个连接字符串文件,创建您的edmx并允许您导入您选择的数据
This should do it: 这应该这样做:
<?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>
... ...
First thing that You need to know. 你需要知道的第一件事。 If You are using a Class Library (.dll), the Entity (.edmx) file created inside the .dll, and You are invoking this Method, from an MVC Application (That have a web.config).
如果您正在使用类库(.dll),则在.dll中创建的实体(.edmx)文件,并且您正在从MVC应用程序(具有web.config)调用此方法。 The connection string inside of the App.config will never be used.
永远不会使用App.config内部的连接字符串。
So You can have the Connection string mapped on the Web.Config (MVC Project), and even delete from the App.Config. 因此,您可以在Web.Config(MVC项目)上映射Connection字符串,甚至可以从App.Config中删除。 It will always use the web.config.
它将始终使用web.config。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.