排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
.net core
前言
操作方法与Framework类似,使用前先下载EF依赖包
Install-Package Microsoft.EntityFrameworkCore.SqlServer -version 3.1.1
先定义数据表的类
//例如有一张学生表 public class Student { public int ID { get; set; } public string LastName { get; set; } public string FirstMidName { get; set; } public DateTime EnrollmentDate { get; set; } public ICollection<Enrollment> Enrollments { get; set; } }
在创建上下文对象的类
此类需要继承 Microsoft.EntityFrameworkCore.DbContext 类
public class SchoolContext : DbContext { public SchoolContext(DbContextOptions<SchoolContext> options) : base(options) { } //上下文对象的类名就是数据库的名称 //添加数据表Students为创建的表名 public DbSet<Student> Students { get; set; } }
提供创建数据库的类
public static class DbInitializer { public static void Initialize(SchoolContext context) { //创建数据库,返回布尔值 var isscuess = context.Database.EnsureCreated(); } }
启动文件中调用创建数据库的方法
//main函数中调用创建数据库 public static void Main(string[] args) { //CreateHostBuilder(args).Build().Run(); var host = CreateHostBuilder(args).Build(); CreateDbIfNotExists(host); host.Run(); } //如果数据库不存在就创建的方法 private static void CreateDbIfNotExists(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var context = services.GetRequiredService<SchoolContext>(); DbInitializer.Initialize(context); } }
配置文件appsettings.json中配置连接数据库的字符串
"ConnectionStrings": { "SchoolContext": "Server=.;Database=SchoolContext;uid=sa;password=123456" },
现在数据库已经创建好了,现在我们来添加数据,添加数据与Framework几乎一模一样
首先添加上下文的依赖,GetConectionString中的参数是配置文件中的参数
services.AddDbContext<SchoolContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SchoolContext")));
在控制器中添加依赖注入,构造函数添加
//定义一个私有只读的实例 private readonly SchoolContext schoolContext; //构造函数注入 public HomeController(SchoolContext _schoolContext) { schoolContext = _schoolContext; }
注入完成过后,往数据库添加数据
//定义一个数组 var students = new Student[] { new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")}, new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")}, new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")}, new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")}, new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")}, new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")}, new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")}, new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")} }; //写入数据缓冲区 foreach (Student s in students) { schoolContext .Students.Add(s); } //真正的写入数据库 schoolContext .SaveChanges();
数据库显示
从数据库读数据,方法与Framework一样
var name = schoolContext.Students.Where(a => a.ID == 5).Select(a => a.FirstMidName).FirstOrDefault();
评价