EF Core 使用教程

视频链接:.NET 6教程,.Net Core 2022视频教程,杨中科主讲_哔哩哔哩_bilibili

Entity Framework Core

EF Core简介

ORM:Object Relational Mapping。让开发者用对象操作的形式操作关系数据库。
ORM负责双向转换
有哪些ORM:EFcore、Dapper、SqlSugar、FreeSql等。

搭建EFcore开发环境

image.png

EF Core 数据的增删改查

image.png

image.png
image.png

image.png
image.png
示例:

internal class Program {
    static async Task Main(string[] args) {
        using (MyDbContext db = new MyDbContext()) {
            //增
            /*
            Person person = new Person();
            person.Name = "李四";
            person.Age = 22;
            person.Salary = 3500;
            person.BirthPlace = "成都";

            db.Persons.Add(person);
            await db.SaveChangesAsync();
            */

            // 查
            /*
            // select * from xxx where Salary>3000
            IQueryable<Person> ps = db.Persons.Where(p => p.Salary > 3000);
            foreach (Person p in ps) {
                Console.WriteLine(p.Name);
            }

            Person person = db.Persons.Single(p => p.Name == "张三");
            Console.WriteLine(person.BirthPlace);

            var ps2 = db.Persons.OrderByDescending(p => p.Salary).Where(p=>p.Salary>2000);
            foreach (Person p in ps2) {
                Console.WriteLine(p.Name+" "+p.Salary);
            }
            */

            // 改、删
            var person = db.Persons.Single(p => p.Name == "张三");
            person.Name = "张三丰";

            var p2 = db.Persons.Single(p => p.Name == "王五");
            db.Persons.Remove(p2);

            await db.SaveChangesAsync();
        }
    }
}
posted @ 2023-06-08 07:58  不爱菠萝的菠萝君  阅读(236)  评论(0编辑  收藏  举报