让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
2014-10-14 00:06
344 查看
获取Sqlite
1.可以用NuGet程序包来获取,它也会自动下载EF6<?xml version="1.0" encoding="utf-8"?> <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> <connectionStrings> <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.data> <DbProviderFactories> <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> <remove invariant="System.Data.SQLite" /> <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" /> </DbProviderFactories> </system.data> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </entityFramework> </configuration>
View Code
其中数据连接串是:
<add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />
注意提供程序集是System.Data.SQLite.EF6。
但是这个配仍然是错误的。
如果此时运行程序,会报错:
Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
或中文错误信息:
未找到具有固定名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序。请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序。
意思是EF没有找到提供System.Data.SQLite.SQLiteFactory的dll,我们看看现在config中的entityFramework节点:
<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </entityFramework>
有System.Data.SQLite.EF6与System.Data.SqlClient,确实没有名称为System.Data.SQLite的提供程序。这里我一直不明白为什么sqlite会去找名称为System.Data.SQLite的提供程序,因为我们在连接串中配置的provider也是System.Data.SQLite.EF6。
那我们就在EF的配置节点中增加一个名为System.Data.SQLite的provider,但type仍然是System.Data.SQLite.EF6。最终的配置如图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/22/0fdf417ac077bb902b3af68628e03dcf.png)
红色部分是配置有变化的地方。
这里再运行程序就可以了。
注意:
1.连接串的配置。
数据连接串可以指定绝对地址,也可以指定相对地址。像我的data source=SqliteTest.db,则SqliteTest.db要在Bin文件夹中,如果是web程序可以通过Data Source=|DataDirectory|\SqliteTest.db来配置在App_Data文件平中。
2.如果没有指定数据库中的表文件名,EF生成的SQL表都是用复数表示。就像我的程序中实体名是Person,但EF去查找的表名会是People。所以在数据库中定义的表名是People。
3.不支持CodeFirst模式,您需要自己先设计好Sqlite的表结构。
示例代码(packages文件太大,所以删除了):SqliteLinqTest.zip
相关文章推荐
- 让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
- Android如何连接和操作SQLite数据库
- 如何用Entity Framework 6 连接Sqlite数据库[转]
- 如何用Entity Framework 6 连接Sqlite数据库
- 如何在ASP.NET MVC5不用EF框架完成对数据库的连接操作
- Navicat for SQLite 如何连接Sqlite数据库
- Android如何使用JDBC连接SQLite数据库?
- 在C#中,如何连接已加密的Sqlite数据库
- 在WINDOWS95&WINDOWS98下如何关闭网络连接
- jsp如何连接数据库!
- 用select如何侦测TCP连接是否被对方关闭?
- 如何在注册表中存储加密的连接字符串
- ado如何连接广域网上的Sql server 数据库 ,请各位大虾指教,很急啊,头都大了
- 如何使用DELPHI和要SQL SERVER数据库连接
- 如何解决客户端与服务器端的连接(mysql) :"xxx.xxx.xxx.xxx" is not allowed to connect to this mysql server.
- 如何增加ORACLE连接数
- jsp如何连接数据库!
- 在eclipse 中如何连接CVS server for windows
- Symbian翻译 之 如何断开ACL连接
- 有没有知道如何连接DB2的数据库?