.net core 创建WebAPI以及使用EF DBFirst框架使用方法与疑问解答(.net 6)

EF语法包:

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
Install-Package Microsoft.EntityFrameworkCore.Tools

生成实体模型:

Scaffold-DbContext "Server=.;Database=h;User ID=admin;Password=123;Trusted_Connection=False;" Microsoft.EntityFrameworkCore.SqlServer

修改实体模型:

Scaffold-DbContext Server=.;Database=h;User ID=admin;Password=123;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Force

把生成的实体和上下文都输出到某个文件夹命令

Scaffold-DbContext "Server=.;Database=h;User ID=admin;Password=123;Trusted_Connection=False;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir {文件夹名}

增加JSON格式脚手架:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

若想增加某个版本json脚手架,需要加入后缀如:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -version 6.0

问题与解决方案:

1、问题:

Your startup project 'XXX' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

解决方案:

给控制器层项目添加包:

Install-Package Microsoft.EntityFrameworkCore.Design

2、问题:

Could not load assembly 'XXX.Model'. Ensure it is referenced by the startup project 'XXX_API'.

解决方案:

此报错是因为EF实体生成的那层没有被其他层引用,将项目引用设置好即可。

3、问题:

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 证书链是由不受信任的颁发机构颁发的。)

解决方案:

在数据库在登录时,更改选项的连接属性,解决方案、信任服务器证书选项都选择或者都不选择,不能只选一个,在VS中连接字符串最后加入以下代码

 Encrypt=True;TrustServerCertificate=True;

4、问题:去除生成实体类时报的警告(强迫症患者使用)

解决方案:

删除生成的上下文实体类中方法

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
#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=h;User ID=admin;Password=123;Encrypt=True;TrustServerCertificate=True;");

然后在此类中添加方法:

public FreigthBillContext(DbContextOptions<FreigthBillContext> options) : base(options) { }

正常来说这是一个默认就有的带参构造函数,以防万一可以搜索一下方法名来确定是否存在,重点在后面带一个base(options)

在Program.cs中添加代码:

builder.Services.AddDbContext<FreigthBillContext>(x=>x.UseSqlServer(builder.Configuration.GetConnectionString("FreigthBill")));

注意UseSqlServer这个方法只有安装下面这个包后才可正常使用,否则会报错找不到这个方法

Install-Package Microsoft.EntityFrameworkCore.SqlServer

(此语句就是第一部分安装EF包中第一条安装语句)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值