微软的 Entity Framework 是一个开源的 对象-关系映射
ORM 框架,它帮忙咱们买通了 数据库的数据模型
到 代码层的畛域模型
,Entity Framework 简化了应用程序对数据库的 CURD 操作,而且还向高层屏蔽了数据是如何长久化到数据库的。
说的具体一点就是 DbContext 充当了数据库到畛域模型之间的桥梁,这篇文章咱们将会探讨如何配置 DbContext 并应用 Entity Framework Core provider 对数据库进行 CURD 操作。
DbContext
DbContext 是 EF 中十分重要的一个组件,它扮演着 Database 的会话连贯,应用它能够查问数据到你的 entitys 汇合中,也能够通过它将 entitys 保留到底层数据库中, EntityFramework Core 中的 DbContext 领有如下几个功能模块。
- 连贯治理
- 查问数据
- 长久化数据
- 批改跟踪
- 缓存
- 事务管理
要想应用 EntityFramework
,须要通过 nuget 援用 Microsoft.EntityFrameworkCore
包,能够通过 Visual Studio 2019 的 NuGet package manager
可视化界面装置 或者 通过 NuGet package manager
命令行工具输出以下命令:
dotnet add package Microsoft.EntityFrameworkCore
接下来探讨下如何在 ASP.Net Core 中应用 DbContext 。
创立 DbContext
首先创立一个 CustomContext
类,并继承 Entity Framework 中的基类 DbContext,如下代码所示:
public class CustomContext : DbContext { public CustomContext(DbContextOptions options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //Write your code here to configure the context } protected override void OnModelCreating(ModelBuilder modelBuilder) { //Write your code here to configure the model } }
能够看到 CustomContext 的构造函数中承受了 DbContextOptions 类型的参数,该类次要用于对 DbContext 做一些必要的参数配置,当然你也能够在 OnConfiguring()
中对 DbContext 进行配置,接下来的 OnModelCreating()
办法用于对 model 进行配置。
上面我在 CustomContext 中新增几个 DbSet<TEntity>
属性用来示意实体汇合,如下代码所示:
public class CustomContext : DbContext { public CustomContext(DbContextOptions options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { } public DbSet<Author> Authors { get; set; } public DbSet<Blog> Blogs { get; set; } } public class Author { public int AuthorID { get; set; } public string AuthorName { get; set; } } public class Blog { public int BlogID { get; set; } public string BlogName { get; set; } public int AuthorID { get; set; } }
注册 DbContext 注入到 ASP.NET Core 运行时
要想在 ASP.NET Core 中应用,须要将 CustomerContext 注入到 ServiceCollection 容器中,这里采纳 SqlServer 作为底层存储,所以还须要在 NuGet 上援用 Microsoft.EntityFrameworkCore.SqlServer
包,接下来在 Startup.ConfigureServices()
中新增如下代码:
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDbContext<CustomContext>(options => options.UseSqlServer("Data Source=.; Initial Catalog=MyTest; Trusted_Connection=Yes")); } }
DbContext 依赖注入
当初 CustomContext 曾经注入到容器了,接下来就能够在 HomeController 中通过依赖注入的形式获取 CustomerContext 实例,上面的代码片段展现了如何去实现。
public class HomeController : Controller { ILogger<HomeController> logger; private CustomContext dbContext; public HomeController(ILogger<HomeController> logger, CustomContext dbContext) { this.logger = logger; this.dbContext = dbContext; dbContext.Database.EnsureCreated(); } }
下面的代码,我用了 dbContext.Database.EnsureCreated();
来确保数据库曾经胜利创立,执行完这句代码之后,数据库将会生成 MyTest 数据库 和 Author,Blog 两张表构造,如下图所示:
接下来在 Index 办法中插入一条记录并查问,成果如下:
这就是配置 EF 所要做的所有事件,当初你能够利用 CustomContext 去所 CURD 操作了,DbContext 在概念上相似 ObjectContext,示意一个 UnitOfWork 组合单元,并且 EF 是DDD畛域的一个实现案例,DbContext 的职责就是负责 应用程序 和 数据库 之间的交互,对于 Entity Framework Core 的更多个性,我会放到前面的文章中和大家一起分享。
译文链接:https://www.infoworld.com/art...
更多高质量干货:参见我的 GitHub: csharptranslate