ef6操作sqlite
博客专区 > fdc2017 的博客 > 博客详情
ef6操作sqlite
fdc2017 发表于8个月前
ef6操作sqlite
  • 发表于 8个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

从项目工具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:\python\actress.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();

共有 人打赏支持
粉丝 0
博文 9
码字总数 1724
×
fdc2017
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: