背景

Entity Framework (EF) Core 是轻量化、可扩展、开源和跨平台版的常用 Entity Framework 数据访问技术。

EF Core 可用作对象关系映射程序 (O/RM),这可以实现以下两点:


  • 使 .NET 开发人员能够使用 .NET 对象处理数据库。
  • 无需再像通常那样编写大部分数据访问代码。

其中EF Core DB First可实现现有数据库自动生成实体、Context。

实现方法

正式开始Database First 开发

1、创建项目

EF Core DB First现有数据库自动生成实体Context_microsoft



EF Core DB First现有数据库自动生成实体Context_连接字符串_02

2、引用Entity Framework (EF) Core 

EF Core DB First现有数据库自动生成实体Context_连接字符串_03


3、程序包管理器控制台执行语句


Scaffold-DbContext "Data Source=.;Initial Catalog=Reptiles1688_UI;Persist Security Info=True;User ID=sa;Password=sa" Microsoft.EntityFrameworkCore.SqlServer -o Models -f
每个包都由其所有者许可给你。NuGet 不负责第三方包,也不授予其许可证。一些包可能包括受其他许可证约束的依赖关系。单击包源(源) URL 可确定任何依赖关系。

程序包管理器控制台主机版本 5.8.0.6930

键入 "get-help NuGet" 可查看所有可用的 NuGet 命令。

PM> Scaffold-DbContext "Data Source=.;Initial Catalog=Reptiles1688_UI;Persist Security Info=True;User ID=sa;Password=sa" Microsoft.EntityFrameworkCore.SqlServer -o Models -f
Build started...
Build succeeded.
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.
PM> Scaffold-DbContext "Data Source=.;Initial Catalog=Reptiles1688_UI;Persist Security Info=True;User ID=sa;Password=sa" Microsoft.EntityFrameworkCore.SqlServer -o Models -f
Build started...
Build succeeded.
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.
PM> Scaffold-DbContext "Data Source=.;Initial Catalog=Reptiles1688_UI;Persist Security Info=True;User ID=sa;Password=sa" Microsoft.EntityFrameworkCore.SqlServer -o Models -f
Build started...
Build succeeded.
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.
PM>

问题

默认生成的会有警告,可以删除没什么影响,也可以对比以前的进行修改

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("Data Source=.;Initial Catalog=Reptiles1688_UI;Persist Security Info=True;User ID=sa;Password=sa");
}
}

解决方法

在 ASP.NET Core 配置系统非常灵活,并且可以将连接字符串存储在 appsettings.json 、环境变量、用户密钥存储或其他配置源中。

例如,你可以使用 机密管理器工具 存储数据库密码,然后在基架中,使用只包含的连接字符串 Name=<database-alias> 。


dotnet user-secrets set ConnectionStrings.YourDatabaseAlias "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=YourDatabase"
dotnet ef dbcontext scaffold Name=ConnectionStrings.YourDatabaseAlias Microsoft.EntityFrameworkCore.SqlServer

下面的示例显示了中存储的连接字符串 appsettings.json 。


{
"ConnectionStrings": {
"BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
},
}

然后,上下文通常在中配置为在 Startup.cs 从配置中读取的连接字符串中。 请注意, GetConnectionString() 方法查找其键为的配置值 ConnectionStrings:<connection string name> 。

 需要导入 Microsoft.Extensions.Configu 命名空间才能使用此扩展方法。


public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BloggingContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}

至此,问题解决。