.NETCore使用EntityFrameworkCore连接数据库生成实体
EF Core 通过数据库提供程序插件模型与 SQL Server/SQL Azure、SQLite、Azure Cosmos DB、MySQL、PostgreSQL 和更多数据库配合使用。
使用EF Core 的优点
Entity Framework (EF) Core 是轻量化、可扩展、开源和跨平台版的常用 Entity Framework 数据访问技术。
EF Core 可用作对象关系映射程序 (O/RM),这可以实现以下两点:
- 使 .NET 开发人员能够使用 .NET 对象处理数据库。
- 无需再像通常那样编写大部分数据访问代码。
在开始使用EF Core的时候我们需要在项目中引用一些包 使用NuGet管理器直接引入即可 包名如下:
Micorsoft.EntityFrameworkCore:EF框架的核心包
Micorsoft.EntityFrameworkCore.SqlServer:针对SqlServer数据库的扩展
其他包是为了生成实体使用的。
EF Core不支持用于可视化设计器的DB模型和向导来创建类似于EF 6的实体和上下文类。所以我们需要使用命令来生成。
Scaffold-DbContext命令
Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>]
[-DataAnnotations] [-Force] [-Project] [-StartupProject] [<CommonParameters>]
参数说明:
1 2 3 4 5 6 7 8 | -OutputDir *** 实体文件所存放的文件目录 -ContextDir *** DbContext文件存放的目录 -Context *** DbContext文件名 -Schemas *** 需要生成实体数据的数据表所在的模式 -Tables *** 需要生成实体数据的数据表的集合 -DataAnnotations -UseDatabaseNames 直接使用数据库中的表名和列名(某些版本不支持) -Force 强制执行,重写已经存在的实体文件 |
在VS2019NuGet程序包管理器控制台运行如下命令:
1 | Scaffold-DbContext "Server=.;Database=DBName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models |
,命令运行成功之后生成如下类 连接字符串在Context类里面
1 2 3 4 5 6 7 8 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263. optionsBuilder.UseSqlServer( @"Server=.;Database=CloudCollector;Trusted_Connection=True;" ); } } |
使用“new”的简单的 DbContext 初始化
在业务逻辑类里面使用using new DbContext 来初始化DbContext
1 2 3 4 5 6 7 8 | public Cloud Get( int id) { using ( var db = new CloudCollectorContext()) { var result = db.Clouds.Where(t => t.Status == 1&&t.Id==id).FirstOrDefault(); return result; } } |
ASP.NET Core 依赖关系注入中的 DbContext
在.NetCore中开发要求需要IOC 所以我们需要在MVC的Startup的ConfigureServices方法中注册DbContext 注册代码如下
1 2 | services.AddDbContext<CloudCollectorContext>( options => options.UseSqlServer( "name=ConnectionStrings:DefaultConnection" )); |
appsettings.json文件配置如下:
1 2 3 | "ConnectionStrings" : { "DefaultConnection" : "Server=.;Database=CloudCollector;Trusted_Connection=True;" } |
如此我们便可以使用依赖注入方式实现DbContext初始化了
1 2 3 4 5 | public Cloud Get( int id) { var result=_context.Clouds.Where(t => t.Status == 1 && t.Id == id).FirstOrDefault(); return result; } |
整个EFCore连接数据库初始化DbContext的方法就这样完成了。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 刚刚!百度搜索“换脑”引爆AI圈,正式接入DeepSeek R1满血版