ef6操作sqlite
发布时间:2020-12-12 19:22:11 所属栏目:百科 来源:网络整理
导读:从项目工具NuGet包工具,下载system.data.sqlite。在App.config中增加如下一条 !--下面是手动增加的一行-- provider invariantName = "System.Data.SQLite" type = "System.Data.SQLite.EF6.SQLiteProviderServices,System.Data.SQLite.EF6" / 写一个自己的
从项目工具NuGet包工具,下载system.data.sqlite。在App.config中增加如下一条 <!--下面是手动增加的一行--> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices,System.Data.SQLite.EF6" /> 写一个自己的实体类要引用 using System.ComponentModel.DataAnnotations.Schema; [Table("Actress")] public class Actress { public Int64 ID { get; set; } public string Name { get; set; } public Int32 Age { get; set; } } 再写一个继承于DBContext的上下文MyContext,引用二条 using System.Data.Entity; public class MyContext : DbContext { public DbSet<Actress> ActressSet { get; set; } public MyContext(DbConnection conn) : base(conn,false) { } } 先用sqliteConnection建立连接,再用sqliteCommand建立一个表,然后就可以用我们的MyContext增删改查,完整代码如下 using (SQLiteConnection conn = new SQLiteConnection()) { conn.ConnectionString = @"Data Source=d:pythonactress.db"; conn.Open(); using (SQLiteCommand cmd = new SQLiteCommand(conn)) { cmd.CommandText = @"create table if not exists Actress (ID integer primary key autoincrement,Name text not null,Age integer not null)"; cmd.ExecuteNonQuery(); using (MyContext context = new MyContext(conn)) { if (context.ActressSet.Where<Actress>(a => a.Name == "王菲").Count<Actress>() == 0) { context.ActressSet.Add(new Actress { Name = "王菲",Age = 47 }); context.ActressSet.Add(new Actress { Name = "范冰冰",Age = 37 }); context.ActressSet.Add(new Actress { Name = "柳岩",Age = 36 }); context.SaveChanges(); } var actress = (from a in context.ActressSet select a).ToList(); actress.ForEach(a => Console.WriteLine($"{a.ID} {a.Name} {a.Age}")); Console.WriteLine("======================================"); context.ActressSet.Add(new Actress { Name = "赵薇",Age=41 }); context.SaveChanges(); actress = (from a in context.ActressSet select a).ToList(); actress.ForEach(a => Console.WriteLine($"{a.ID} {a.Name} {a.Age}")); context.ActressSet.Remove(context.ActressSet.Find(4)); ; context.SaveChanges(); Console.WriteLine("======================"); actress = (from a in context.ActressSet select a).ToList(); actress.ForEach(a => Console.WriteLine($"{a.ID} {a.Name} {a.Age}")); } } } Console.WriteLine("按任意键结束"); Console.ReadKey(); 小 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Cisco 3750文件系统学习总结
- vue 组件高级用法实例详解
- ruby-on-rails – 如何在Rails脚手架生成器上强制使用单个表
- 直接将XML存入到SQL中(SQL2008)
- ruby – Capybara RSpec,spec / features dir被rspec忽略.命
- c – 在不使用文字的情况下检测负数的更好方法是什么?
- ruby-on-rails-3 – 无法使用nested_form_for gem
- ruby-on-rails – i18n和Date :: ABBR_DAYNAMES
- linux系统下MongoDB单节点安装教程
- Error parsing XML: junk after document element