Skip to content

Commit

Permalink
feat: Imprime posts e seus autores ordenados por data
Browse files Browse the repository at this point in the history
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
renebentes committed Sep 21, 2022
1 parent 2cf6346 commit d73eb1e
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions Blog/Program.cs
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();

0 comments on commit d73eb1e

Please sign in to comment.