企业建站系统还有没有前景可言,wordpress 免费中文模板,织梦个人网站模版,企业品牌营销型网站建设微软的 Entity Framework 是一个开源的 对象-关系映射 ORM 框架#xff0c;它帮助我们打通了 数据库的数据模型 到 代码层的领域模型#xff0c;Entity Framework 简化了应用程序对数据库的 CURD 操作#xff0c;而且还向高层屏蔽了数据是如何持久化到数据库的。说的具体一点… 微软的 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 中新增几个 DbSetTEntity 属性用来表示实体集合如下代码所示public class CustomContext : DbContext{public CustomContext(DbContextOptions options) : base(options){}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){}protected override void OnModelCreating(ModelBuilder modelBuilder){}public DbSetAuthor Authors { get; set; }public DbSetBlog 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.AddDbContextCustomContext(options options.UseSqlServer(Data Source.; Initial CatalogMyTest; Trusted_ConnectionYes));}}DbContext 依赖注入 现在 CustomContext 已经注入到容器了接下来就可以在 HomeController 中通过依赖注入的方式获取 CustomerContext 实例下面的代码片段展示了如何去实现。public class HomeController : Controller{ILoggerHomeController logger;private CustomContext dbContext;public HomeController(ILoggerHomeController 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/article/3311737/how-to-use-the-dbcontext-in-entity-framework-core.html