1、添加引用Nuget包
Microsoft.EntityFrameworkCore.SqliteMicrosoft.EntityFrameworkCore.DesignMicrosoft.EntityFrameworkCore.Tools.DotNet
2、创建数据库上下文
using Microsoft.EntityFrameworkCore;using System.Collections.Generic;namespace ConsoleApp.SQLite{public class BloggingContext : DbContext{public DbSet<Blog> Blogs { get; set; }public DbSet<Post> Posts { get; set; }protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlite("Data Source=blogging.db");}}public class Blog{public int BlogId { get; set; }public string Url { get; set; }public List<Post> Posts { get; set; }}public class Post{public int PostId { get; set; }public string Title { get; set; }public string Content { get; set; }public int BlogId { get; set; }public Blog Blog { get; set; }}}
Startup里注入:
services.AddDbContext<BloggingContext>();3、数据库迁移
dotnet ef migrations add InitialCreate//提交到数据库dotnet ef database update
4、发布
这里仅说一下注意事项,我们在开发调试的时候,默认生成的数据库文件所在目录为:
bin/Debug/netcoreapp1.1/blogging.db发布后,应该把数据库文件拷贝到发布程序的根目录里,不然报错,找不到数据库文件。
参考
https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite
https://github.com/dotnet/efcore/tree/main/test/EFCore.Sqlite.Tests

本文暂时没有评论,来添加一个吧(●'◡'●)