我試圖連接到Azure的SQL,但我得到的錯誤The underlying provider failed on Open
我的連接字符串有什麼問題(無法通過ConfiguratioManager訪問它)?
我發現,我已經把連接字符串中的錯誤app.config
- 它移動到可執行項目的app.config
,但還是同樣的結果
當我檢查ConfigurationManager.ConnectionStrings["AzureDatabase"]
時,它返回null。當我嘗試連接它時,只使用默認的SQLExpress。
當我檢查我的可執行應用程序的生成文件夾 - 有一個<app_name>.exe.config
文件與「AzureDatabase」。我卡在哪裏從這裏
搜索這是我的DbContext類
[DbConfigurationType(typeof(AzureDbConfiguration))]
public class ProductDbContext : DbContext
{
//Db sets
static MyContext()
{
//I don't want to change database from code
Database.SetInitializer<MyContext>(null);
}
public MyContext() : base("AzureDatabase") //this is my connectionstring
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//some mappings
}
}
這是AzureDbConfiguration
public class AzureDbConfiguration : DbConfiguration
{
public AzureDbConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient",() => new SqlAzureExecutionStrategy(2, TimeSpan.FromSeconds(10)));
}
}
這是我的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="AzureDatabase"
connectionString="Server=tcp:xxx.database.windows.net,1433;Initial Catalog=xxx;Persist Security Info=False;User ID=xxxx;Password=xxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
關於這裏有什麼問題的任何想法?
你命名你的連接字符串'AzureDatabase'並檢查'ConfigurationManager.ConnectionStrings [「MyConnectionString」]'?這種命名不對齊嗎?如果將它命名爲'DefaultConnection',它會起作用嗎? –
@drediske啊,對不起 - 它是'AzureDatabase'而不是'MyConnectionString'。更新了我的帖子... – Prokurors