建立EntityFrameworkCore项目的模板

92 字
1 分钟
建立EntityFrameworkCore项目的模板

建立通用 AppHost项目

先在 Program.cs中配置好数据库连接

builder.AddNpgsqlDbContext<TasksContext>("Default-PostgreSQL", configureDbContextOptions: option =>
{
});

然后在 appsettings.json中添加数据库连接字符串

{
// ...
"ConnectionStrings": {
"Default-PostgreSQL": "..."
}
}

编写 DbContext 如下

using Microsoft.EntityFrameworkCore;
public class ExampleContext(DbContextOptions options) : DbContext(options)
{
public DbSet<ExampleRecordType> Records { get; set; }
}

使用 dotnet-tools初始化数据库(建立初始化迁移)

Terminal window
dotnet ef migrations add InitialCreate
dotnet database update

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
建立EntityFrameworkCore项目的模板
https://blog.truebigsand.top/posts/ef-core-project-template/
作者
真-大沙子
发布于
2026-06-30
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
真-大沙子
编程 · 数学 · 二次元 · 生活
公告
音乐
封面

音乐

暂未播放

0:000:00
暂无歌词
分类
标签
站点统计
文章
45
分类
5
标签
51
总字数
42,438
运行时长
0
最后活动
0 天前
站点信息
构建平台
GitHub Actions
博客版本
Firefly v6.13.5
文章许可
CC BY-NC-SA 4.0

文章目录