C#的EF6操作sqlite 数据库

配置

<?xml version="1.0" encoding="utf-8"?>
  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>

<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" />
  
   <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" />
</DbProviderFactories>

</system.data>

<add name="SqliteTest" connectionString="data source=E:\retail.db" providerName="System.Data.SQLite.EF6" />

core first 模式

using SQLite.CodeFirst;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;

namespace WindowsFormsApp1
{
    public class SqliteDbContext : DbContext
    {
        //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)
        //使用“SqliteDbContext”连接字符串。默认情况下,此连接字符串针对您的 LocalDb 实例上的
        //“WindowsFormsApp1.SqliteDbContext”数据库。
        // 
        //如果您想要针对其他数据库和/或数据库提供程序,请在应用程序配置文件中修改“SqliteDbContext”
        //连接字符串。
        public SqliteDbContext()
            : base("name=SqliteTest")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var intit = new SqliteCreateDatabaseIfNotExists<SqliteDbContext>(modelBuilder);
            Database.SetInitializer(intit);
            Database.SetInitializer(new SqliteDropCreateDatabaseWhenModelChanges<SqliteDbContext>(modelBuilder));
            modelBuilder.Entity<MyEntity>();
            base.OnModelCreating(modelBuilder);
        }
        //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First  模型
        //的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。

        public virtual DbSet<MyEntity> MyEntities { get; set; }
    }

    public class MyEntity
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        public DateTime Time { get; set; }
    }
}

引用的nuget
在这里插入图片描述
尽可能版本选择高一点,否则没有SqliteDropCreateDatabaseWhenModelChanges 这个方法将会很痛苦。

以上就是主要代码

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在C#中将实体类映射到SQLite数据库,可以使用ORM(对象关系映射)框架,比如EF Core和Dapper等。以下是使用EF Core的一个简单示例: 1. 首先,需要安装EF Core和SQLite包。可以使用NuGet包管理器或手动下载并添加到项目中。 2. 创建SQLite数据库文件并打开它。可以使用SQLite Studio或其他SQLite客户端。 3. 创建一个实体类,例如: ```csharp public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } ``` 4. 创建DbContext类,指定连接字符串和实体类: ```csharp using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Person> People { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Data Source=database.db"); } } ``` 5. 在C#代码中,使用DbContext连接到SQLite数据库,并将实体类映射到数据库表。以下是一个示例代码: ```csharp using System.Linq; //连接到SQLite数据库 using (var db = new MyDbContext()) { //创建Person表 db.Database.EnsureCreated(); //插入新记录 var person = new Person { Name = "John Doe", Age = 30 }; db.People.Add(person); db.SaveChanges(); //查询记录 var people = db.People.ToList(); //输出结果 foreach (var p in people) { Console.WriteLine($"Id: {p.Id}, Name: {p.Name}, Age: {p.Age}"); } } ``` 这样,就可以将实体类映射到SQLite数据库中,可以方便地进行CRUD操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值