[英]WPF/EF/SQLite The server was not found or was not accessible
為此,我已經拉了好幾個小時了。 我是C#等.NET的新手,所以如果這很瑣碎,我深表歉意。
我正在嘗試將WPF與Entity Framework + SQLite一起使用。
我設法通過手動執行以下操作連接到SQLite數據庫:
var connection = ConfigurationManager.ConnectionStrings["AccountHelper.Properties.Settings.AccountHelperConnectionString"].ConnectionString;
m_dbConnection = new SQLiteConnection(connection);
executeReader();
我收到以下錯誤:
建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:SQL網絡接口,錯誤:50-發生本地數據庫運行時錯誤。指定的LocalDB實例不存在。
我真的不知道如何解決這個問題。 真令人沮喪!
編輯:我用一個教程回答了此問題,以正確配置所有內容。
因此,要回答我上面的直接問題,問題是TextContext不知道連接字符串。 因此,如果您為Context提供與字符串值相同的名稱,它將刪除此錯誤消息。
也就是說,配置完全錯誤。 我設法使其正常工作。 因為我很努力,所以我將分享我所做的努力。
逐步指南進行配置
配置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 Studio2012。它可以工作,但是在項目中創建ADO.NET項時遇到問題。
在此網頁上,找不到舊版本,您將必須手動修改URL以獲取所需的版本。
在此網頁上,請確保選擇的版本具有“這是唯一能夠安裝Visual Studio 2013設計時組件的安裝程序包。”是否為Visual Studio 2013。
安裝exe,並確保在安裝過程中勾選與設計器相關的組件。
2)創建一個新的WPF項目。 同樣,如果您使用已修改的項目,則可能會遇到一些配置問題。 您最好開始一個新項目,進行配置,一旦知道如何做,就將其移植到您的項目中。
3)通過塊管理器安裝Entity框架。
4)手動添加(不要使用NUGGET MANAGER !!!),您可以在程序集->擴展中找到以下參考:
System.Data.SQLite核心
System.Data.SQLite設計器
實體框架的System.Data.SQLite
LINQ的System.Data.SQLite
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="W:\visual studio projects\AccountHelper\AccountHelper\Data\AccountHelper.sqlite3"'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
7)你很好。
您可以在此鏈接上看到項目源代碼。 檢查“首次提交”中的代碼,其中包含您需要的所有內容。 其他提交是特定於項目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.