Skip to content

Commit

Permalink
feat: Altera dados do autor através do post
Browse files Browse the repository at this point in the history
Com o Tracking ativo, o EF permite que alteremos
dados de todo os conjuntos e subconjuntos, sem a
necessidade de acessar ao BD novamente. Para tan-
to, precisamos apenas chamar os métodos corres-
pondente a operação (update, add) e confirmar o
salvamento no BD.
  • Loading branch information
renebentes committed Oct 6, 2022
1 parent 376d18c commit 3ead3ba
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions Blog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@

using var context = new BlogDataContext();

var posts = context
var post = context
.Posts
.AsNoTracking()
// Efetua o JOIN
.Include(p => p.Author)
.Include(p => p.Category)
.OrderByDescending(p => p.LastUpdateDate)
.ToList();
.FirstOrDefault();

foreach (var post in posts)
{
Console.WriteLine($"{post.Title} escrito por {post.Author?.Name}");
}
post.Author.Name = "Teste";

context.Update(post);
context.SaveChanges();

0 comments on commit 3ead3ba

Please sign in to comment.