Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseInMemoryDatabase("StudentDB");
}
}
class Program
{
static void Main()
{
using (var context = new AppDbContext())
{
// CREATE
context.Students.Add(new Student { Name = "Alice", Age = 20 });
context.SaveChanges();
// READ
var students = context.Students.ToList();
foreach (var s in students)
{
Console.WriteLine($"{s.Id} - {s.Name} - {s.Age}");
}
// UPDATE
var student = context.Students.First();
student.Age = 25;
context.SaveChanges();
// DELETE
context.Students.Remove(student);
context.SaveChanges();
}
}
}Coding works best on desktop or with an external keyboard.