WPF/EF/SQLite 找不到服务器或无法访问服务器



我已经为此拔头发好几个小时了。我是 C# et .NET 的新手,所以如果这是微不足道的事情,我深表歉意。

我正在尝试将WPF与实体框架+ SQLite一起使用。

我已经设法通过手动执行以下操作连接到SQLite数据库:

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

我收到以下错误:

在以下情况下发生与网络相关或特定于实例的错误 建立与 SQL Server 的连接。找不到服务器或 无法访问。验证实例名称是否正确,以及 SQL Server 配置为允许远程连接。(提供程序:SQL 网络接口,错误: 50 - 发生本地数据库运行时错误。 指定的本地数据库实例不存在。

我真的不知道如何解决这个问题。这真的很令人沮丧!

编辑:我用一个教程回答了这个问题,以正确配置一切。

因此,为了回答我上面的直接问题,问题是 TextContext 不知道连接字符串。因此,如果您为上下文指定与字符串值相同的名称,它将删除此错误消息。

也就是说,配置是完全错误的。我设法让它工作。因为我很挣扎,所以我会分享我为让它发挥作用所做的一切。

配置它的分步指南

配置 WPF 项目以使用实体框架 (ADO.NET) 和 SQLite

配置这个可能真的很棘手,如果你以前尝试过其他方法,甚至更棘手。所以我建议完全卸载您可能已安装的任何SQLite软件。

1) 根据您的 Visual Studio 版本,安装 sqlite-netFx45-setup-bundle-x86-2012-1.0.93.0.exe 或更高版本。您可以在 sqlite.org 上找到它。几点:

    我知道 1.0.98.0
  • 和 1.0.99.0 不适用于 Visual Studio 2012。它可以工作,但在项目中创建 ADO.NET 项时遇到问题。

  • 在此网页上,您找不到旧版本,您必须手动修改 URL 才能获得所需的版本。

  • 在此网页上,请确保选择的版本是否为"这是唯一能够安装 Visual Studio 2013 的设计时组件的安装包"。

  • 安装
  • exe 并确保在安装过程中勾选设计器相关组件。

2) 创建一个新的 WPF 项目。同样,如果您使用已修改的项目,则可能会遇到一些配置问题。你最好开始一个新项目,做配置,一旦你知道怎么做,就把它移植到你的项目中。

3) 通过掘金管理器安装实体框架。

4) 手动添加(不使用掘金管理器!!)以下引用,您可以在程序集 -> 扩展中找到:

  • System.Data.SQLite Core

  • System.Data.SQLite Designer

  • System.Data.SQLite for Entity Framework

  • System.Data.SQLite for LINQ

5)向这个项目添加一个新项目 ->数据 -> ADO.NET blabla。根据需要命名并创建。您需要为其创建连接。在这里,如果您没有兼容的SQLite版本,您将无法创建SQLite连接。

6) 然后确保您的 App.config 如下所示,您可能需要更改提供程序:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="AccountHelperEntities" connectionString="metadata=res://*/AccountHelperModel.csdl|res://*/AccountHelperModel.ssdl|res://*/AccountHelperModel.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;W:visual studio projectsAccountHelperAccountHelperDataAccountHelper.sqlite3&quot;'" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

7)你很好去。

您可以在此链接上看到项目源代码。检查第一次提交的代码,它包含您需要的所有内容。其他提交是特定于项目的。

最新更新