为什么这个应用程序尝试连接到SQL Server而不是SQLite? [英] Why this app is trying to connect to SQL Server instead of SQLite?

查看:76
本文介绍了为什么这个应用程序尝试连接到SQL Server而不是SQLite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在关注 Microsoft官方教程,以使用SQLite创建名为"c5>"的代码优先应用.使用下面显示的配置,我收到有关无法连接到SQL Server的错误.但是,正如您在下面的App.config文件中看到的那样,这里没有涉及SQL Server吗?

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?

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

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

错误:

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>

推荐答案

替换字符串DataDirectory仅在ASP.net Web应用程序中现成"存在.如果要在WPF中使用它-您需要这样设置:

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");

这里是另一个 SO链接 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆