EF語(yǔ)法包:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
Install-Package Microsoft.EntityFrameworkCore.Tools
生成實(shí)體模型:
Scaffold-DbContext "Server=.;Database=h;User ID=admin;Password=123;Trusted_Connection=False;" Microsoft.EntityFrameworkCore.SqlServer
修改實(shí)體模型:
Scaffold-DbContext Server=.;Database=h;User ID=admin;Password=123;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Force
把生成的實(shí)體和上下文都輸出到某個(gè)文件夾命令
Scaffold-DbContext "Server=.;Database=h;User ID=admin;Password=123;Trusted_Connection=False;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir {文件夾名}
增加JSON格式腳手架:
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
若想增加某個(gè)版本json腳手架,需要加入后綴如:
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -version 6.0
問(wèn)題與解決方案:
1、問(wèn)題:
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.
解決方案:
給控制器層項(xiàng)目添加包:
Install-Package Microsoft.EntityFrameworkCore.Design
2、問(wèn)題:
Could not load assembly 'XXX.Model'. Ensure it is referenced by the startup project 'XXX_API'.
解決方案:
此報(bào)錯(cuò)是因?yàn)镋F實(shí)體生成的那層沒(méi)有被其他層引用,將項(xiàng)目引用設(shè)置好即可。
3、問(wèn)題:
A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 證書鏈?zhǔn)怯刹皇苄湃蔚念C發(fā)機(jī)構(gòu)頒發(fā)的。)
解決方案:
在數(shù)據(jù)庫(kù)在登錄時(shí),更改選項(xiàng)的連接屬性,解決方案、信任服務(wù)器證書選項(xiàng)都選擇或者都不選擇,不能只選一個(gè),在VS中連接字符串最后加入以下代碼
Encrypt=True;TrustServerCertificate=True;
4、問(wèn)題:去除生成實(shí)體類時(shí)報(bào)的警告(強(qiáng)迫癥患者使用)
解決方案:
刪除生成的上下文實(shí)體類中方法
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) { }
正常來(lái)說(shuō)這是一個(gè)默認(rèn)就有的帶參構(gòu)造函數(shù),以防萬(wàn)一可以搜索一下方法名來(lái)確定是否存在,重點(diǎn)在后面帶一個(gè)base(options)
在Program.cs中添加代碼:
builder.Services.AddDbContext<FreigthBillContext>(x=>x.UseSqlServer(builder.Configuration.GetConnectionString("FreigthBill")));
注意UseSqlServer這個(gè)方法只有安裝下面這個(gè)包后才可正常使用,否則會(huì)報(bào)錯(cuò)找不到這個(gè)方法文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-604640.html
Install-Package Microsoft.EntityFrameworkCore.SqlServer
(此語(yǔ)句就是第一部分安裝EF包中第一條安裝語(yǔ)句)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-604640.html
到了這里,關(guān)于.net core 創(chuàng)建WebAPI以及使用EF DBFirst框架使用方法與疑問(wèn)解答(.net 6)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!