Entity Framework App.Config 中的连接字符串
Entity Framework Connection String in App.Config
我是 Entity Framework 的新手,如果这个问题很基础,我深表歉意。
我正在阅读一本 Code First 教科书,并创建了一个小型 class 库 (c#) 和一个控制台应用程序。教科书上说当我运行它Entity Framework会自动在SQL服务器上建立数据库。它没有,我相信的原因是因为我有一个命名实例而不是默认实例 \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 并允许您导入您选择的数据
应该这样做:
<?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>
...
您需要知道的第一件事。如果您正在使用 Class 库 (.dll)、在 .dll 中创建的实体 (.edmx) 文件,并且您正在从 MVC 应用程序(具有 web.config)调用此方法. App.config 中的连接字符串永远不会被使用。
因此您可以将连接字符串映射到 Web.Config(MVC 项目),甚至从 App.Config 中删除。它将始终使用 web.config.
我是 Entity Framework 的新手,如果这个问题很基础,我深表歉意。
我正在阅读一本 Code First 教科书,并创建了一个小型 class 库 (c#) 和一个控制台应用程序。教科书上说当我运行它Entity Framework会自动在SQL服务器上建立数据库。它没有,我相信的原因是因为我有一个命名实例而不是默认实例 \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 并允许您导入您选择的数据
应该这样做:
<?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>
...
您需要知道的第一件事。如果您正在使用 Class 库 (.dll)、在 .dll 中创建的实体 (.edmx) 文件,并且您正在从 MVC 应用程序(具有 web.config)调用此方法. App.config 中的连接字符串永远不会被使用。
因此您可以将连接字符串映射到 Web.Config(MVC 项目),甚至从 App.Config 中删除。它将始终使用 web.config.