Skip to content

Commit

Permalink
infra config
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrmee committed Jun 18, 2023
1 parent ed1f839 commit 41267ff
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
35 changes: 2 additions & 33 deletions src/Infrastructure/BookStoreDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Domain.Models;
using Infrastructure.Interceptors;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Infrastructure.Configuration;

namespace Infrastructure;

Expand All @@ -15,39 +16,7 @@ public BookStoreDbContext(DbContextOptions<BookStoreDbContext> options, Auditabl
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Book>(entity =>
{
entity.HasMany(e => e.OrderDetails)
.WithOne(e => e.Book)
.HasPrincipalKey(e => e.Id)
.HasForeignKey(e => e.BookId)
.IsRequired();
entity.HasMany(e => e.BookCategories)
.WithOne(e => e.Book)
.HasPrincipalKey(e => e.Id)
.HasForeignKey(e => e.BookId)
.IsRequired();
});

modelBuilder.Entity<Category>(entity =>
{
entity.HasMany(e => e.BookCategories)
.WithOne(e => e.Category)
.HasPrincipalKey(e => e.Id)
.HasForeignKey(e => e.BookId)
.IsRequired();
});

modelBuilder.Entity<User>(entity =>
{
entity.HasMany(e => e.Orders)
.WithOne(e => e.User)
.HasPrincipalKey(e => e.UserName)
.HasForeignKey(e => e.UserName)
.IsRequired();
});
ModelBuilderConfiguration.Apply(modelBuilder);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Expand Down
23 changes: 23 additions & 0 deletions src/Infrastructure/Configuration/BookConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Domain.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Configuration;

public class BookConfiguration : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.HasMany(e => e.OrderDetails)
.WithOne(e => e.Book)
.HasPrincipalKey(e => e.Id)
.HasForeignKey(e => e.BookId)
.IsRequired();

builder.HasMany(e => e.BookCategories)
.WithOne(e => e.Book)
.HasPrincipalKey(e => e.Id)
.HasForeignKey(e => e.BookId)
.IsRequired();
}
}
17 changes: 17 additions & 0 deletions src/Infrastructure/Configuration/CategoryConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Domain.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Configuration;

public class CategoryConfiguration : IEntityTypeConfiguration<Category>
{
public void Configure(EntityTypeBuilder<Category> builder)
{
builder.HasMany(e => e.BookCategories)
.WithOne(e => e.Category)
.HasPrincipalKey(e => e.Id)
.HasForeignKey(e => e.BookId)
.IsRequired();
}
}
13 changes: 13 additions & 0 deletions src/Infrastructure/Configuration/ModelBuilderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Configuration;

public class ModelBuilderConfiguration
{
public static void Apply(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new UserConfiguration());
modelBuilder.ApplyConfiguration(new CategoryConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
}
}
17 changes: 17 additions & 0 deletions src/Infrastructure/Configuration/UserConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Domain.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Configuration;

public class UserConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasMany(e => e.Orders)
.WithOne(e => e.User)
.HasPrincipalKey(e => e.UserName)
.HasForeignKey(e => e.UserName)
.IsRequired();
}
}

0 comments on commit 41267ff

Please sign in to comment.