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

背景

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

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

  • 使 .NET 开发人员能够使用 .NET 对象处理数据库。

  • 无需再像通常那样编写大部分数据访问代码。

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

实现方法

正式开始Database First 开发

1、创建项目

图片

 

 

图片

2、引用Entity Framework (EF) Core 

图片

 

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 -fBuild 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 -fBuild 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 -fBuild 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")));}

至此,问题解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值