为什么这个应用程序试图连接到SQL Server而不是SQLite?

问题描述 投票:0回答:1

环境VS2019.NET 4.8EF 6SQLiteWPF App (.NET Framework)

我正在按照此Microsoft official tutorial创建一个使用SQLite的代码优先应用程序,名为WPF_EF6。使用下面显示的配置,我收到有关无法连接到SQL Server的错误。但是,正如您在下面的App.config文件中所看到的,这里没有涉及SQL Server?

问题:我在这里可能想念什么?以及如何解决该问题?应用程序可以正常编译,但会抛出此运行时错误。我了解错误的内容,但为什么要查找SQL Server?错误发生在以下using块的第一行:

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; }
    }
}

错误

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
1个回答
0
投票

替换字符串DataDirectory仅对于ASP.net Web应用程序是“开箱即用”的。如果要在WPF中使用它-您需要这样设置:

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

这里是另一个SO linkDocs

© www.soinside.com 2019 - 2024. All rights reserved.