samedi 23 janvier 2016

WPF/EF/SQLite The server was not found or was not accessible

I have been pulling my hair for many hours on this. I'm new to C# et .NET so I apologize if this is something trivial.

I'm trying to use WPF with Entity framework + SQLite.

I have managed to connect to the SQLite database by manually doing:

var connection = ConfigurationManager.ConnectionStrings["AccountHelper.Properties.Settings.AccountHelperConnectionString"].ConnectionString;
m_dbConnection = new SQLiteConnection(connection);
executeReader();

So the connection string is correct.

I have added:

  • a data connection in the server explorer

  • a ADO.NET Entity Data Model (.edmx) (but then removed it, same issue)

  • a Data source (DataSet) (but then removed it, same issue)

My App.config is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://ift.tt/1eigFsq -->
  </configSections>
  <connectionStrings>
    <add name="AccountHelper.Properties.Settings.AccountHelperConnectionString" connectionString="data source=&quot;W:\visual studio projects\AccountHelper\AccountHelper\AccountHelper.sqlite3&quot;" providerName="System.Data.SQLite.EF6" />
    <add name="AccountHelperEntities" connectionString="metadata=http://res*/AccountHelperSQLite.csdl|res*/AccountHelperSQLite.ssdl|res*/AccountHelperSQLite.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;W:\visual studio projects\AccountHelper\AccountHelper\AccountHelper.sqlite3&quot;'" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

My MainWindow.xaml.cs looks like:

public partial class MainWindow : Window 
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            TestContext context = new TestContext();
            context.Users.OrderBy(c => c.name).Load();
            this.dataGrid.ItemsSource = context.Users.Local;
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

public class tUser
{
    public long id { get; set; }
    public string name { get; set; }
}
public class TestContext : DbContext
{
    public DbSet<tUser> Users { get; set; }
}

You can find the rest of the project on the Git repository.

I get 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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.

I really have no clue on how to fix this. It is really frustrating!

Aucun commentaire:

Enregistrer un commentaire