Skip to content

Commit b0f83dc

Browse files
committed
feat(Data): aplica mapeamento objeto-relacionado com EF Core
1 parent 45f1607 commit b0f83dc

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
using BlazingShop.Models;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace BlazingShop.Data;
6+
7+
public class AppDbContext(DbContextOptions<AppDbContext> options)
8+
: DbContext(options)
9+
{
10+
public DbSet<Category> Categories => Set<Category>();
11+
12+
public DbSet<Product> Products => Set<Product>();
13+
14+
protected override void OnModelCreating(ModelBuilder modelBuilder)
15+
{
16+
base.OnModelCreating(modelBuilder);
17+
18+
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using BlazingShop.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
4+
5+
namespace BlazingShop.Data.Configurations;
6+
7+
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
8+
{
9+
public void Configure(EntityTypeBuilder<Category> builder)
10+
{
11+
builder.ToTable(nameof(Category));
12+
13+
builder.HasKey(c => c.Id);
14+
15+
builder.Property(c => c.Title)
16+
.IsRequired();
17+
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using BlazingShop.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
4+
5+
namespace BlazingShop.Data.Configurations;
6+
7+
public class ProductConfiguration : IEntityTypeConfiguration<Product>
8+
{
9+
public void Configure(EntityTypeBuilder<Product> builder)
10+
{
11+
builder.ToTable(nameof(Product));
12+
13+
builder.HasKey(p => p.Id);
14+
15+
builder.Property(p => p.Title)
16+
.IsRequired();
17+
18+
builder.Property(p => p.Price)
19+
.IsRequired();
20+
21+
builder.HasOne(p => p.Category)
22+
.WithMany(p => p.Products);
23+
}
24+
}

src/BlazingShop/Models/Category.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public Category(int id, string title)
1414
public int Id { get; set; }
1515

1616
public string Title { get; set; } = string.Empty;
17+
18+
public IEnumerable<Product> Products { get; set; } = [];
1719
}

0 commit comments

Comments
 (0)