-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Imprime posts e seus autores ordenados por data
O JOIN só é aplicado quando utilizado o método `Include()`, é uma boa prática sempre usar o operador de null safe `?` em objetos relacionados.
- Loading branch information
1 parent
2cf6346
commit d73eb1e
Showing
1 changed file
with
11 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,19 @@ | ||
using Blog.Data; | ||
using Blog.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
using var context = new BlogDataContext(); | ||
|
||
var user = new User | ||
{ | ||
Name = "André Baltieri", | ||
Slug = "andrebaltieri", | ||
Email = "[email protected]", | ||
Bio = "9x Microsoft MVP", | ||
Image = "https://balta.io", | ||
PasswordHash = "123098457" | ||
}; | ||
|
||
var category = new Category { Title = "Backend", Slug = "backend" }; | ||
var posts = context | ||
.Posts | ||
.AsNoTracking() | ||
// Efetua o JOIN | ||
.Include(p => p.Author) | ||
.OrderByDescending(p => p.LastUpdateDate) | ||
.ToList(); | ||
|
||
var post = new Post | ||
foreach (var post in posts) | ||
{ | ||
Author = user, | ||
Category = category, | ||
Body = "<p>Hello world</p>", | ||
Slug = "comecando-com-ef-core", | ||
Summary = "Neste artigo vamos aprender EF core", | ||
Title = "Começando com EF Core", | ||
CreateDate = DateTime.Now, | ||
LastUpdateDate = DateTime.Now, | ||
}; | ||
Console.WriteLine($"{post.Title} escrito por {post.Author?.Name}"); | ||
} | ||
|
||
context.Posts.Add(post); | ||
context.SaveChanges(); | ||
|