mirror of
https://github.com/ivanpaulovich/clean-architecture-manga.git
synced 2025-01-08 11:57:36 +08:00
Page:
Entity Framework Core
2
Entity Framework Core
Ivan Paulovich edited this page 2019-12-03 16:10:23 +01:00
Table of Contents
public sealed class MangaContext : DbContext
{
public MangaContext(DbContextOptions options) : base(options)
{
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Credit> Credits { get; set; }
public DbSet<Debit> Debits { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>()
.ToTable("Account");
modelBuilder.Entity<Account>()
.Ignore(p => p.Credits)
.Ignore(p => p.Debits);
modelBuilder.Entity<Customer>()
.ToTable("Customer")
.Property(b => b.SSN)
.HasConversion(
v => v.ToString(),
v => new SSN(v));
modelBuilder.Entity<Customer>()
.ToTable("Customer")
.Property(b => b.Name)
.HasConversion(
v => v.ToString(),
v => new Name(v));
modelBuilder.Entity<Customer>()
.Ignore(p => p.Accounts);
modelBuilder.Entity<Debit>()
.ToTable("Debit")
.Property(b => b.Amount)
.HasConversion(
v => v.ToAmount().ToDecimal(),
v => new PositiveAmount(v));
modelBuilder.Entity<Credit>()
.ToTable("Credit")
.Property(b => b.Amount)
.HasConversion(
v => v.ToAmount().ToDecimal(),
v => new PositiveAmount(v));
modelBuilder.Entity<Customer>().HasData(
new { Id = new Guid("197d0438-e04b-453d-b5de-eca05960c6ae"), Name = new Name("Test User"), SSN = new SSN("19860817-9999") }
);
modelBuilder.Entity<Account>().HasData(
new { Id = new Guid("4c510cfe-5d61-4a46-a3d9-c4313426655f"), CustomerId = new Guid("197d0438-e04b-453d-b5de-eca05960c6ae") }
);
modelBuilder.Entity<Credit>().HasData(
new
{
Id = new Guid("f5117315-e789-491a-b662-958c37237f9b"),
AccountId = new Guid("4c510cfe-5d61-4a46-a3d9-c4313426655f"),
Amount = new PositiveAmount(400),
Description = "Credit",
TransactionDate = DateTime.UtcNow
}
);
modelBuilder.Entity<Debit>().HasData(
new
{
Id = new Guid("3d6032df-7a3b-46e6-8706-be971e3d539f"),
AccountId = new Guid("4c510cfe-5d61-4a46-a3d9-c4313426655f"),
Amount = new PositiveAmount(400),
Description = "Debit",
TransactionDate = DateTime.UtcNow
}
);
}
}
Add Migration
Run the EF Tool to add a migration to the Infrastructure
project.
dotnet ef migrations add "InitialCreate" -o "EntityFrameworkDataAccess/Migrations" --project src/Infrastructure --startup-project src/WebApi
Update Database
Generate tables and seed the database via Entity Framework Tool:
dotnet ef database update --project src/Infrastructure --startup-project src/WebApi
Index of Clean Architecture Manga
Home
Use Cases
Flow of Control
Architecture Styles
Design Patterns
Domain-Driven Design Patterns
- Value Object
- Entity
- Aggregate Root
- Repository
- Use Case
- Bounded Context
- Entity Factory
- Domain Service
- Application Service
Separation of Concerns
Encapsulation
Test-Driven Development TDD
Fakes
SOLID
- Single Responsibility Principle
- Open-Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
.NET Core Web API
- Swagger and API Versioning
- Microsoft Extensions
- Feature Flags
- Logging
- Data Annotations
- Authentication
- Authorization
Entity Framework Core
Environment Configurations
DevOps
- Running the Application Locally
- Running the Tests Locally
- Continuous Integration & Continuous Deployment