Why this app is trying to connect to SQL Server instead of SQLite?

0

Environment: VS2019, .NET 4.8, EF 6, SQLite, WPF App (.NET Framework)

I am following this Microsoft official tutorial to create a code-first app, called WPF_EF6 using SQLite. With the configuration shown below, I am getting an error about failing to connect to SQL Server. But as you can see in the App.config file below, there is no SQL Server is involved here?

Question: what may I be missing here? And how can we resolve the issue? App compiles fine but throws this runtime error. I understand the content of the error, bu why it's looking for SQL Server? The error occurs at the first line inside of the following using block:

using (MathDocsContext db = new MathDocsContext())
{
    db.MyObjs.Add(new MyObj { Author = "Test1 author", Title = "Test1 title"});
    db.SaveChanges();
}

MyDbContex.cs:

namespace WPF_EF6.Model
{
    public class MyDbContex : DbContext
    {
        public MyDbContex() : base("MySQLiteDb")
        {
        }

        public DbSet<MyObj> MyObjs { get; set; }
    }
}

Error:

System.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=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. Cannot create an automatic instance. See the Windows Application event log for error details.

App.config

<?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"/>
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SQLite.EF6" />
            <add name="SQLite Data Provider (Entity Framework 6)"
                 invariant="System.Data.SQLite.EF6"
                 description=".NET Framework Data Provider for SQLite (Entity Framework 6)"
                 type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
            <remove invariant="System.Data.SQLite" />
            <add name="SQLite Data Provider" invariant="System.Data.SQLite"
                 description=".Net Framework Data Provider for SQLite"
                 type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
        </DbProviderFactories>
    </system.data>
    <connectionStrings>
        <add name="SqlLiteContext" 
             connectionString="Data Source=|DataDirectory|MySQLiteDb.sqlite" 
             providerName="System.Data.SQLite" />
    </connectionStrings>
    <entityFramework>
        <providers>
            <provider invariantName="System.Data.SQLite.EF6"
                      type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
            <provider invariantName="System.Data.SQLite"
                      type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
        </providers>
    </entityFramework>
</configuration>
c#
entity-framework
sqlite
entity-framework-6
ef-code-first
asked on Stack Overflow May 28, 2020 by nam

1 Answer

0

Substitution string DataDirectory exist "out-of-the-box" only for ASP.net web application. If you want to use it within WPF - you need to set it like this:

AppDomain.CurrentDomain.SetData("DataDirectory", @"C:\Some\Known\Path");

Here is another SO link and Docs.

answered on Stack Overflow May 28, 2020 by vasily.sib

User contributions licensed under CC BY-SA 3.0