static void Main(string[] args)
{
using (var db = new AppDbContext())
{
// ایجاد پایگاهداده
db.Database.EnsureCreated();
// افزودن دادهها
var author = new Author { Name = "J.K. Rowling" };
var book1 = new Book { Title = "Harry Potter and the Sorcerer's Stone", Author = author };
var book2 = new Book { Title = "Harry Potter and the Chamber of Secrets", Author = author };
db.Authors.Add(author);
db.Books.AddRange(book1, book2);
db.SaveChanges();
// خواندن و نمایش دادهها
var authors = db.Authors
.Include(a => a.Books)
.ToList();
foreach (var a in authors)
{
Console.WriteLine($"{a.Name} has written:");
foreach (var b in a.Books)
{
Console.WriteLine($" - {b.Title}");
}
}
}
}
هر Author میتواند چندین Book داشته باشد.
public class Author {
}
public class Book {
}
protected override void OnModelCreating(ModelBuilder modelBuilder) {
}
عملیات CRUD :
class Program {
}