[翻译 EF Core in Action 2.2] 创建应用程序的数据库上下文

Entity Framework Core in Action

Entityframework Core in action是 Jon P smith 所著的关于Entityframework Core 书籍。原版地址. 是除了官方文档外另一个学习EF Core的不错途径, 书中由浅入深的讲解的EF Core的相关知识。因为没有中文版,所以本人对其进行翻译。 预计每两天一篇更新 PS: 翻译难免限于本人水平有不准确的地方,建议英文水平不错的同学直接查看原版,有不足的地方欢迎指正

第一部分目录导航

创建应用程序的数据库上下文

访问数据库前我们需要以下操作:

  • 定义应用程序的数据库上下文,创建类并继承EF Core的DbContext类
  • 访问数据库时,需要创建该类的实例

本章后面所有的数据库查询都是这些步骤,下面我会详细的描述这些步骤

定义应用程序的数据库上下文: EfCoreDbContext

使用EF Core的关键类就是应用程序的DbContext,该类继承EF Core的DbContext类. 添加各种属性代表数据库中的表. 它还可以覆盖基类方法使用EF Core的其他功能,比如配置数据库建模等. 图2.6展示了应用程序数据库上下文的概述并指出了重要的部分

832799-20190425185134334-707015186.png

图2.6中你并没有看到上一节提到的Review实体类和BookAuthor中间类的DbSet属性,不用惊讶,因为这两个实体类都只能通过Book类访问(这是一种设计思想,其实放在里面也没关系)

注: 上面没有介绍到配置数据库建模,在第六章与第七章中详细介绍了如何对数据库建模

创建应用程序数据库上下文实例

在第一章中我们在OnConfiguring方法中调用了UseSqlServer方法并硬编码了连接字符串,缺点很明显连接字符串是固定的,所以我们现在使用另一种方法. 我们希望使用不同的数据库用于开发和单元测试.

注: 第十五章介绍了对使用EF Core的应用程序进行单元测试

下图展示了创建应用程序数据库上下文时的选项,图中的代码是我在单元测试时使用的. 在第五章中介绍了在ASP.NET Core应用程序中使用EF Core,使用依赖注入的方式为应用程序提供数据库上下文的实例

832799-20190425185005984-724548352.png

图的最后,使用了using语句包裹了创建数据库上下文实例的代码,因为DbContext实现了IDisposable接口. context变量就是我们需要的数据库上下文实例

创建应用程序的数据库

使用EF Core创建数据库有好几种方法,大多数都会选择使用EF Core的迁移. 使用应用程序的数据库上下文和实体类作为数据库结构的模型. Add-Migration命令进行数据库建模,然后使用模型构建命令创建对应的数据库

注: 如果你使用的代码是从Github仓库下载的,那么不需要执行我们下面要用的迁移命令

迁移的好处是可以将代码中所做的任何更改来更新数据库,如果更改实体类或数据库上下文配置,使用Add-Migration命令可以创建最新的迁移文件

使用迁移,我们需要在应用程序的启动项安装Microsoft.EntityFramework.Tools的NuGet包,它的功能是让你在包管理控制台(PMC)使用迁移命令

  • Add-Migration MyMigrationName 创建迁移命令,它会将所有更改(添加)的实体和等其他建模配置创建一个迁移文件, MyMigrationName是迁移的名称
  • Update-Database 应用迁移命令,如果不存在数据库会数据一个数据库,如果已存在命令会检查数据库的迁移版本并更新到应用程序中的最新版本(也有命令可以更新到指定版本)

注: 还可以使用EF Core的CLI运行这些命令 (参见https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/cli/dotnet),第十一章列出了命令的列表. .Net Core2.1引入了全局工具可以直接运行命令

有另一种方法调用Update-Database命令,就是在应用程序的启动代码中调用context.Database.Migrate方法. 这种方法对托管的ASP.NET Core Web应用程序非常有用,第五章详细介绍了这种方法与其中的一些限制

注: 虽然EF Core的迁移功能很好用,但它并未涵盖所有类型的数据库结构更改. 对于某些项目据库据库不由EF Core定义和管理数据库,这意味着无法使用EF Core的迁移功能. 第十一章讨论了数据库迁移的选项以及它们的优缺点

如果是分层应用程序怎么办

一般的应用程序都是分层的,主网站是一个项目,数据访问层又是一个项目. 这时Add-Migration命令就要复杂一点

在图书应用程序中,应用程序的数据库上下文在名为DataLayer的项目中,ASP.NET Core Web程序在名为EfCoreInAction的项目中(我会在后面讲到为什么这么分层). 要添加迁移命令如下

Add-Migration Chapter02 -Project DataLayer -StartupProject EfCoreInAction

图书应用程序的数据库上下文 EfCoreContext 类没有无参构造函数,Add-Migration命令会失败,要解决这个潜在的问题,我们需要创建一个IDesignTimeDbContextFactory<T>接口的实现类,因为Add-Migration命令会去查找这个接口的实现. 这个实现类创建应用程序的数据库上下文的正确配置实现,使Add-Migration命令可以正常工作. 这也是在图书应用程序中所做的. 参见 https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/configuring-dbcontext#using-idesigntimedbcontextfactorytcontext

转载于:https://www.cnblogs.com/LiangSW/p/10770290.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Summary Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. Following the crystal-clear explanations, real-world examples, and around 100 diagrams, you'll discover time-saving patterns and best practices for security, performance tuning, and unit testing. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology There's a mismatch in the way OO programs and relational databases represent data. Enti ty Framework is an object-relational mapper (ORM) that bridges this gap, making it radically easier to query and write to databases from a .NET application. EF creates a data model that matches the structure of your OO code so you can query and write to your database using standard LINQ commands. It will even automatically generate the model from your database schema. About the Book Using crystal-clear explanations, real-world examples, and around 100 diagrams, Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. You'l start with a clear breakdown of Entity Framework, long with the mental model behind ORM. Then you'll discover time-saving patterns and best practices for security, performance tuning, and even unit testing. As you go, you'll address common data access challenges and learn how to handle them with Entity Framework. What's Inside Querying a relational database with LINQ Using EF Core in business logic Integrating EF with existing C# applications Applying domain-driven design to EF Core Getting the best performance out of EF Core Covers EF Core 2.0 and 2.1
Summary Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. Following the crystal-clear explanations, real-world examples, and around 100 diagrams, you'll discover time-saving patterns and best practices for security, performance tuning, and unit testing. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology There's a mismatch in the way OO programs and relational databases represent data. Entity Framework is an object-relational mapper (ORM) that bridges this gap, making it radically easier to query and write to databases from a .NET application. EF creates a data model that matches the structure of your OO code so you can query and write to your database using standard LINQ commands. It will even automatically generate the model from your database schema. About the Book Using crystal-clear explanations, real-world examples, and around 100 diagrams, Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. You'l start with a clear breakdown of Entity Framework, long with the mental model behind ORM. Then you'll discover time-saving patterns and best practices for security, performance tuning, and even unit testing. As you go, you'll address common data access challenges and learn how to handle them with Entity Framework. What's Inside Querying a relational database with LINQ Using EF Core in business logic Integrating EF with existing C# applications Applying domain-driven design to EF Core Getting the best performance out of EF Core Covers EF Core 2.0 and 2.1

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值