加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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;
using System.Data.Common;

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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读