.NET Core Entity Framework Core 创建数据库
自动创建数据库必须在NuGet 中安装一下EFCore库
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
数据迁移常用命令
dotnet ef migrations add NewColum --新增migrations
dotnet ef database update--跟新数据库
dotnet ef migrations add Addrs--新增一个migrations
dotnet ef database update
dotnet ef datebase update NewColum--根据newcolum跟新数据库
dotnet ef migrations remove--删除最新未使用的migrations
1、.NET Core中手动执行命令创建
dotnet ef migrations add MyFirstMigration
dotnet ef database update
2、通过.NET Core代码,首次使用时自动创建
获取context
string connectionString = "数据库链接字符串";
var builder = new DbContextOptionsBuilder<CustomDbContxt>()
.UseSqlServer(connectionString);
var context = new CustomDbContxt(builder.Options);
1)如果已经创建了迁移,则可以使用:
context.Database.Migrate();
使用添加迁移的表和数据创建数据库
2)如果不迁移数据,只是需要在首次运行时,则可以使用:
context.Database.Migrate();
3)如果需要检查数据库是否存在,则可以使用:
bool exists = (context.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists();
4)如果需要删除数据库,则可以使用:
context.Database.EnsureDeleted();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-08-29 C# 选择文件 选择文件夹 无响应 闪退问题
2022-08-29 C# Word替换文本
2022-08-29 C# PDF加密
2022-08-29 C# 生成压缩包
2022-08-29 C# 读取XML数据
2022-08-29 C# Word文档转PDF