Monday, June 5, 2017

Sqlite on Azure: The server was not found or was not accessible

Leave a Comment

I am using SQLite in an Asp.Net MVC 5 project which is working fine.

I uploaded the application to Microsoft Azure and I got the following error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.  The server was not found or was not accessible.  Verify that the instance name is correct and that SQL Server is configured to allow remote connections.  (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 

In Azure I added the following connection string:

Data Source=D:\home\site\wwwroot\Data\MyDatabase.sqlite;Password=pass 

Since there is no SqlLite database option I selected Custom: enter image description here

I checked using Azure Kudu and the file is in the right path ...

However, when I run the application I keep getting the error.

Does anyone knows how to solve this?

UPDATE

My Entity Framework class is the following:

[DbConfigurationType(typeof(Configuration))] public class Context : DbContext, IDbContext {    public Context() {     Configuration.With(x => {               x.AutoDetectChangesEnabled = true;       x.LazyLoadingEnabled = false;       x.ProxyCreationEnabled = true;       x.ValidateOnSaveEnabled = true;     });   }    public DbSet<Post> Posts { get; set; }   public DbSet<Tag> Tags { get; set; }    protected override void OnModelCreating(DbModelBuilder builder) {   }  }  public class Configuration : DbConfiguration {   public Configuration() {     SetDatabaseInitializer<Context>(null);         SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);   } } 

And I have the following on Global.asax:

AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory + "Data");  builder.RegisterType<Context>().As<IDbContext>().InstancePerRequest(); 

The connection string in Web.Config is:

<connectionStrings>   <add name="Context" providerName="System.Data.SQLite" connectionString="Data Source=|DataDirectory|\MyDatabase.sqlite;Version=3;Password=pass" /> </connectionStrings> 

1 Answers

Answers 1

I found the solution. Instead of defining a ConnectionString in Azure I need to define an Application Setting in Azure for DataDirectory where the value is the directory in Azure where the database is ...

The absolute path of the database location in Azure can be found using Kudu's Debug Console.

Following my example the DataDirectory application settings would have:

Key: DataDirectory Value: D:\home\site\wwwroot\Data 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment