首页 / SQLITE / 应用EF访问SQLite数据
应用EF访问SQLite数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了应用EF访问SQLite数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5269字,纯文字阅读大概需要8分钟。
内容图文
public class MyContext : DbContext { public DbSet<Person> Persons { get; set; } public MyContext() : base("SqliteTest") { } }
public class Person { public int Id { get; set; } public string Name { get; set; } public DateTime BirthDay { get; set; } }
4、修改配置文件
在安装 "System.Date.SQLite(x86/x64)" 时,会默认在类库项目的app.config文件中加入一些配置信息,但是由于现在我们需要通过控制台项目来访问类库项目的方法来访问数据,所以需要把配置信息从类库项目复制到控制台项目中。并将app.config文件从类库项目中删除。控制台项目中的配置信息如下所示:
<?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> <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" /> </DbProviderFactories> </system.data> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <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>
这些信息都是在安装SQLite时自动配置的,由于我们在MyContext定义数据连接字符串名称为"SqliteTest",所以需要中配置文件中加入连接字符串信息。
<connectionStrings> <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" /> </connectionStrings>
5、在控制台项目中通过MyContext访问数据
(1) 引用 "Netage.Data.SQLite"类库项目
(2) 引入其他DLL
(3) 通过MyContext访问数据
class Program { static void Main(string[] args) { using (var context = new MyContext()) { Console.WriteLine(context.Persons.Count()); } Console.WriteLine("运行结束"); Console.ReadKey(); } }
(4)运行控制台项目
(5)手动把安装包中的"packages\System.Data.SQLite.Core.1.0.103\build\net45" 中的x64及x86复制到控制台项目的Bin\Debug目录中,如下所示。
(6) 再次运行控制台项目
(7)再次修改配置文件,修改的配置已经被红色部分标识,如下所示:
<?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> <system.data> <DbProviderFactories> <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" /> <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.SqlConnectionFactory, EntityFramework" /> <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" /> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </entityFramework> <connectionStrings> <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" /> </connectionStrings> </configuration>
(8) 再次运行应用程序,如下所示:
这是因为EF SQLite不支持CodeFirst模式,因为到目前为止,我们还没有创建数据库及表结构,所以才有此错误。
通过SQLite Expert创建数据库及表结构
1、创建数据库,指定数据库文件名及文件存放位置
2、创建表结构,添加列,并设置列的属性
3、测试表创建完成,可以再自己尝试去操作这个工具的其他功能,这里不再细说。
应用EF访问SQLite数据库数据
因为上面已经将数据库文件存放到了别的位置 ,可以需要修改配置文件以特定新创建的数据库。
<add name="SqliteTest" connectionString="data source=C:\Users\paulhuang\Desktop\SQLiteTest" providerName="System.Data.SQLite.EF6" />
这时我们就可以书写LINQ代码来测试相应的功能了。
1、插入数据
2、修改数据
关于通过EF访问SQLite的基本操作基本上到这里了,如果有不正确的地方欢迎指正。
应用EF访问SQLite数据
标签:ide count() mis 应用程序 packages .net 其他 line 测试表
本文系统来源:http://www.cnblogs.com/hehheai/p/6513789.html
内容总结
以上是互联网集市为您收集整理的应用EF访问SQLite数据全部内容,希望文章能够帮你解决应用EF访问SQLite数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。