基于 .NET Core 的 EF Core 入门整理(基于SQL Server)
Entity Framework (EF) Core 是轻量化、可扩展和跨平台版的常用 Entity Framework 数据访问技术。
EF Core 可用作对象关系映射程序 (O/RM),以便于 .NET 开发人员能够使用 .NET 对象来处理数据库,这样就不必经常编写大部分数据访问代码了。
一、安装Entity Framework Core Nuget包
install-package Micorsoft.EntityFrameworkCore 安装EF框架的核心包
install-package Micorsoft.EntityFrameworkCore.SqlServer 安装SQL Server 提供程序
install-package Microsoft.EntityFrameworkCore.Design 安装用户根据现有的数据库生成模型代码等
install-package Microsoft.EntityFrameworkCore.Tools 安装包管理器控制台工具
二、使用实例
1、安装Nuget包后,创建实体类
1 using System.Collections.Generic; 2 using System.ComponentModel.DataAnnotations; 3 using System.ComponentModel.DataAnnotations.Schema; 4 5 namespace WebApplication.DataModels 6 { 7 8 [Table("Blog")] 9 public class Blog 10 { 11 12 //主键 13 [Key] 14 public int ID { get; set; } 15 16 public string Name { get; set; } 17 18 public ICollection<Post> Posts { get; set; } //子集合 19 } 20 21 [Table("Post")] 22 public class Post 23 { 24 [Key] 25 public int ID { get; set; } 26 27 public string Name { get; set; } 28 29 public int BlogId { get; set; } //外键 30 31 public Blog Blog { get; set; } //关联类 32 } 33 }
2、创建上下文、注入数据库信息
1 using Microsoft.EntityFrameworkCore; 2 3 namespace WebApplication.DataModels 4 { 5 6 public class SQLite:DbContext 7 { 8 public DbSet<Blog> Blogs { get; set; } 9 public DbSet<Post> Posts { get; set; } 10 11 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 12 { 13 optionsBuilder.UseSqlServer(@"Server=.;Database=DotNetCore;Trusted_Connection=True;"); 14 } 15 } 16 }
或方式二
//创建上下文
1 using Microsoft.EntityFrameworkCore; 2 3 namespace WebApplication.DataModels 4 { 5 public class SQLite : DbContext 6 { 7 public SQLite(DbContextOptions<SQLite> opt) : base(opt) 8 { 9 10 } 11 12 public DbSet<Blog> Blogs { get; set; } 13 public DbSet<Post> Posts { get; set; } 14 15 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 16 { 17 //配置数据链接 18 //optionsBuilder.UseSqlServer(@"Server=.;Database=DotNetCore;Trusted_Connection=True;"); 19 } 20 } 21 }
1 using Microsoft.EntityFrameworkCore; 2 3 //配置EF的服务注册 4 public void ConfigureServices(IServiceCollection services) 5 { 6 services.AddDbContext<SQLite>(options => options.UseSqlServer(Configuration["ConnectionString:SqlServer"])); 7 }
3、迁移创建数据库
//工具 =》 NutGet包管理器 =》程序包管理器 add-migration InitialCreate //以为迁移搭建基架,并为模型创建一组初始表。 update-database //数据库迁移命令
4、实例化测试
1 var count = 0; 2 //实例化 3 using (SQLite dBContext = new SQLite()) 4 { 5 var blog = new Blog 6 { 7 Name = "日志", 8 Posts = new List<Post> 9 { 10 new Post{ Name="Post"} 11 } 12 }; 13 dBContext.Blogs.Add(blog); 14 //运行结果count=2 15 count = dBContext.SaveChanges(); 16 };
或在方式二的情况下需通过依赖注册
public class HomeController : Controller { private SQLite dBContext; public HomeController(SQLite db) { dBContext = db; } public IActionResult Index() { var count = 0; var blog = new Blog { Name = "日志", Posts = new List<Post> { new Post{ Name="Post"} } }; dBContext.Blogs.Add(blog); //运行结果count=2 count = dBContext.SaveChanges(); return Content(count.ToString()); } }
PS:个人见解,欢迎各位评论交流。

浙公网安备 33010602011771号