Skip to content

Instantly share code, notes, and snippets.

@samueleresca
Created May 10, 2019 14:15
Show Gist options
  • Save samueleresca/c77e4b256b6fde75f16e52f3e5951013 to your computer and use it in GitHub Desktop.
Save samueleresca/c77e4b256b6fde75f16e52f3e5951013 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using VinylStore.Catalog.Domain.Entities;
namespace VinylStore.Catalog.Infrastructure.SchemaDefinitions
{
public class ItemEntitySchemaDefinition : IEntityTypeConfiguration<Item>
{
public void Configure(EntityTypeBuilder<Item> builder)
{
builder.ToTable("Items", CatalogContext.DEFAULT_SCHEMA);
builder.HasKey(it => it.Id);
builder.Property(x => x.Name)
.IsRequired();
builder.Property(x => x.Description)
.IsRequired()
.HasMaxLength(1000);
builder.OwnsOne(root => root.Artist, b =>
{
b.WithOwner()
.HasForeignKey(e => e.ArtistId);
});
builder.OwnsOne(root => root.Genre, b =>
{
b.WithOwner()
.HasForeignKey(e => e.GenreId);
});
builder.OwnsOne(root => root.Price);
builder.Property(x => x.Price).HasConversion(
x => $"{x.Amount}:{x.Currency}",
x => new Money
{
Amount = Convert.ToDecimal(x.Split(':', StringSplitOptions.None)[0]),
Currency = x.Split(':', StringSplitOptions.None)[1]
});
}
public class ArtistEntitySchemaConfiguration : IEntityTypeConfiguration<Artist>
{
public void Configure(EntityTypeBuilder<Artist> builder)
{
builder.ToTable("Artists", CatalogContext.DEFAULT_SCHEMA);
builder.HasKey(x => x.ArtistId);
builder.Property(x => x.ArtistId);
builder.Property(x => x.ArtistName)
.IsRequired()
.HasMaxLength(200);
builder.HasMany(navigation => navigation.Items);
}
}
public class GenreEntitySchemaConfiguration : IEntityTypeConfiguration<Genre>
{
public void Configure(EntityTypeBuilder<Genre> builder)
{
builder.ToTable("Genres", CatalogContext.DEFAULT_SCHEMA);
builder.HasKey(x => x.GenreId);
builder.Property(x => x.GenreId);
builder.Property(x => x.GenreDescription)
.IsRequired()
.HasMaxLength(1000);
builder.HasMany(navigation => navigation.Items);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment