Skip to content

Commit

Permalink
feat(data): Mapeia relacionamentos de Post
Browse files Browse the repository at this point in the history
O modelo Post tem relacionamento com Category
e User, portanto, precisamos mapeá-lo de for-
ma efetiva.
  • Loading branch information
renebentes committed Nov 2, 2022
1 parent e4bcdac commit d76b905
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Blog/Data/Mappings/PostMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,16 @@ public void Configure(EntityTypeBuilder<Post> builder)

builder.HasIndex(it => it.Slug, "IX_Post_Slug")
.IsUnique();

// Relacionamentos
builder.HasOne(it => it.Author)
.WithMany(it => it.Posts)
.HasConstraintName("FK_Post_Author")
.OnDelete(DeleteBehavior.Cascade);

builder.HasOne(it => it.Category)
.WithMany(it => it.Posts)
.HasConstraintName("FK_Post_Category")
.OnDelete(DeleteBehavior.Cascade);
}
}
2 changes: 2 additions & 0 deletions Blog/Models/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Blog.Models;

public class Category : ModelBase
{
public IList<Post> Posts { get; set; } = new List<Post>();

public string Slug { get; set; } = string.Empty;

public string Title { get; set; } = string.Empty;
Expand Down
2 changes: 2 additions & 0 deletions Blog/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public class User : ModelBase

public string PasswordHash { get; set; } = string.Empty;

public IList<Post> Posts { get; set; } = new List<Post>();

public string Slug { get; set; } = string.Empty;
}

0 comments on commit d76b905

Please sign in to comment.