static async Task Main(string[] args)
{
using TextDbContext db = new TextDbContext();//建立我们刚刚创立的实体映射类
var b = db.Books.Single(b => b.Title == "黄帝内经");//查询语句类似SQL
db.Remove(b);
await db.SaveChangesAsync();
Console.WriteLine("6");
}
1.一般们用EFCore 操作数据库时最好是用 FLuent API
但在学习之前我们一定不能忘记要用Nuget导入需要准备的依赖包

2.创建一到几个实体类如下图,我们先建立一个Book类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Book
{
public long Id { get; set; }
public string Title { get; set; }
public DateTime Pubtime { get; set; }
public double Price { get; set; }
public string AuthorName { get; set; }
}
}
3.有了实体类之后,我们接下来需要写一个配置类去规定我们实体在数据里的数据存储状态。包括设置一些主键啊,规定某个字段的大小之类。这是第四步的作用!
internal class BookEntityConfig : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.ToTable("T_Books");//表示对应数据库里的表叫什么名字
//其他操作暂不赘述大家可以自行查阅相关资料
}
}
4.我们需要建立一个建立继承DbContext实体类和数据库之间的映射关系,简单来说就是需要一个类来管理他
namespace ConsoleApp1
{ //Add-Migration initialCreate
// Update-database
internal class TextDbContext:DbContext
{
public DbSet<Book> Books { get; set; } //只需要注意Book要和实体类名对应
public DbSet<Person> Persons{ get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
//这里写你的数据库配置链接字符串其他的照抄就好了
string conn = "Server=.;Database=demo1;Trusted_Connection=True;Integrated Security=True;TrustServerCertificate=True";
optionsBuilder.UseSqlServer(conn) ;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
}
}
5.启动程序包管理控制台,输入以下命令就行,自己到数据库查看结果是否成功
1.Add-Migration initialCreate
2.Update-database
6.非常的完美!!!学废了吗?嘿嘿!
二:添加数据
class Program
{
static async Task Main(string[] args)
{
string str;
using TextDbContext db = new TextDbContext();//建立我们刚刚创立的实体映射类
//建立一些列实体类数据,用var去自动识别实体变量非常方便
var book1 = new Book
{
AuthorName = "往常下",
Title = "麝香基础",
Price = 59.8,
Pubtime = new DateTime(2016, 4, 4)
};
var book2 = new Book
{
AuthorName = "凌志玲",
Title = "黄帝内经",
Price = 59.8,
Pubtime = new DateTime(2011, 2, 28)
};
var book3= new Book
{
AuthorName = "坤坤",
Title = "及你太美",
Price = 9.99,
Pubtime = new DateTime(2019, 1, 4)
};
var book4 = new Book
{
AuthorName = "杨中科",
Title = "零基础学c#",
Price = 59.8,
Pubtime = new DateTime(2011, 3, 4)
};
var book5 = new Book
{
AuthorName = "石昊",
Title = "雷迪报数",
Price = 100,
Pubtime = new DateTime(2029, 3, 4)
};
//照抄就行,非常简单
db.Books.Add(book1);
db.Books.Add(book2);
db.Books.Add(book3);
db.Books.Add(book4);
db.Books.Add(book5);
//异步方法提高并发量
await db.SaveChangesAsync();
//验证代码运行是否成功
Console.WriteLine("6");
}
}
运行一下,不亏是我的IKun
三:修改数据
我们来做个示范查询一下黄帝内经这边书!把他的作者坤坤改为倪海夏老师
代码如下:
class Program
{
static async Task Main(string[] args)
{
using TextDbContext db = new TextDbContext();//建立我们刚刚创立的实体映射类
var b = db.Books.Single(b => b.Title == "黄帝内经");//查询语句类似SQL
b.AuthorName = "倪海夏";//修改值
await db.SaveChangesAsync();
Console.WriteLine("6");
}
}
结果:
四:删除数据
删除叫黄帝内经的书
class Program
{
static async Task Main(string[] args)
{
using TextDbContext db = new TextDbContext();//建立我们刚刚创立的实体映射类
var b = db.Books.Single(b => b.Title == "黄帝内经");//查询语句类似SQL
b.AuthorName = "倪海夏";//修改值
await db.SaveChangesAsync();
Console.WriteLine("6");
}
}
五:查询数据
简单查一下书名和大于50价格书籍的信息
using TextDbContext db = new TextDbContext();//建立我们刚刚创立的实体映射类
foreach(Book book in db.Books)
{
Console.WriteLine($"title={book.Title},Author={book.AuthorName}");
}
IEnumerable<Book> books = db.Books.Where(b => b.Price > 50);
foreach (Book b in db.Books)
{
Console.WriteLine($"title={b.Title},Author={b.AuthorName},Price={b.Price}");
}
结果