Je suis en train de se connecter à SQL Azure, mais j'obtiens l'erreur The underlying provider failed on Open
Quel est le problème avec ma chaîne de connexion (ne peut pas y accéder via ConfiguratioManager)?
J'ai découvert que j'ai mis chaîne de connexion dans le mauvais app.config
- déplacé à app.config
de projet exécutable, mais toujours le même résultat
Lorsque je vérifie ConfigurationManager.ConnectionStrings["AzureDatabase"]
, il renvoie null. Et quand j'essaie de me connecter, il utilise simplement SQLExpress par défaut.
Lorsque je vérifie le dossier de construction de mon application exécutable - il y a un fichier <app_name>.exe.config
avec "AzureDatabase". Je suis coincé sur l'endroit où la recherche d'ici
Ceci est ma classe 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
}
}
C'est AzureDbConfiguration
public class AzureDbConfiguration : DbConfiguration
{
public AzureDbConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient",() => new SqlAzureExecutionStrategy(2, TimeSpan.FromSeconds(10)));
}
}
Ceci est mon fichier 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>
Des idées sur ce qui ne va pas ici?
Vous avez appelé votre chaîne de connexion 'AzureDatabase' et vérifiez' ConfigurationManager.ConnectionStrings ["MyConnectionString"] '? Cette dénomination ne s'aligne-t-elle pas? Cela fonctionne-t-il si vous le nommez DefaultConnection'? –
@drediske Ah, désolé - c'est 'AzureDatabase' au lieu de' MyConnectionString'. Mise à jour de mon message ... – Prokurors