【发布时间】:2020-05-28 05:21:14
【问题描述】:
环境:VS2019、.NET 4.8、EF 6、SQLite、WPF App (.NET Framework)
我正在关注这个Microsoft official tutorial 来创建一个代码优先应用程序,名为WPF_EF6 使用SQLite。使用下面显示的配置,我收到有关无法连接到 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