码迷,mamicode.com
首页 > 数据库 > 详细

EntityFramework连接SQLite

时间:2015-03-21 13:54:36      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

EF很强大,可惜对于SQLite不支持CodeFirst模式(需要提前先设计好数据库表结构),不过对SQLite的数据操作还是很好用的。

先用SQLiteManager随便创建一个数据库和一张表:

技术分享

通过NuGet安装 EF6 和System.Data.SQLite(会自动把其他三个也装上)

技术分享

添加配置内容:

数据库连接

1   <connectionStrings>
2     <add name="StudentContext" connectionString="Data Source=StudentDb.sqlite" providerName="System.Data.SQLite.EF6"/>
3   </connectionStrings>

配置文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3   <configSections>
 4     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
 5     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 6   </configSections>
 7   <connectionStrings>
 8     <add name="StudentContext" connectionString="Data Source=StudentDb.sqlite" providerName="System.Data.SQLite.EF6"/>
 9   </connectionStrings>
10   <startup>
11     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
12   </startup>
13   <entityFramework>
14     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
15     <providers>
16       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
17       <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
18       <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
19     </providers>
20   </entityFramework>
21   <system.data>
22     <DbProviderFactories>
23       <remove invariant="System.Data.SQLite.EF6" />
24       <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" />
25       <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
26     </DbProviderFactories>
27   </system.data>
28 </configuration>

测试一下:

技术分享

EntityFramework连接SQLite

标签:

原文地址:http://www.cnblogs.com/Ares945/p/EF.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!