Advertisement
Guest User

Untitled

a guest
May 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.EntityFrameworkCore.Metadata.Builders;
  5. using VinylStore.Catalog.Domain.Entities;
  6.  
  7. namespace VinylStore.Catalog.Infrastructure.SchemaDefinitions
  8. {
  9. public class ItemEntitySchemaDefinition : IEntityTypeConfiguration<Item>
  10. {
  11. public void Configure(EntityTypeBuilder<Item> builder)
  12. {
  13.  
  14. builder.ToTable("Items", CatalogContext.DEFAULT_SCHEMA);
  15. builder.HasKey(it => it.Id);
  16.  
  17. builder.Property(x => x.Name)
  18. .IsRequired();
  19.  
  20. builder.Property(x => x.Description)
  21. .IsRequired()
  22. .HasMaxLength(1000);
  23.  
  24. builder.OwnsOne(root => root.Artist, b =>
  25. {
  26. b.WithOwner()
  27. .HasForeignKey(e => e.ArtistId);
  28. });
  29. builder.OwnsOne(root => root.Genre, b =>
  30. {
  31. b.WithOwner()
  32. .HasForeignKey(e => e.GenreId);
  33. });
  34. builder.OwnsOne(root => root.Price);
  35.  
  36. builder.Property(x => x.Price).HasConversion(
  37. x => $"{x.Amount}:{x.Currency}",
  38. x => new Money
  39. {
  40. Amount = Convert.ToDecimal(x.Split(':', StringSplitOptions.None)[0]),
  41. Currency = x.Split(':', StringSplitOptions.None)[1]
  42. });
  43.  
  44. }
  45.  
  46.  
  47. public class ArtistEntitySchemaConfiguration : IEntityTypeConfiguration<Artist>
  48. {
  49. public void Configure(EntityTypeBuilder<Artist> builder)
  50. {
  51. builder.ToTable("Artists", CatalogContext.DEFAULT_SCHEMA);
  52. builder.HasKey(x => x.ArtistId);
  53. builder.Property(x => x.ArtistId);
  54.  
  55. builder.Property(x => x.ArtistName)
  56. .IsRequired()
  57. .HasMaxLength(200);
  58.  
  59. builder.HasMany(navigation => navigation.Items);
  60.  
  61. }
  62. }
  63.  
  64. public class GenreEntitySchemaConfiguration : IEntityTypeConfiguration<Genre>
  65. {
  66. public void Configure(EntityTypeBuilder<Genre> builder)
  67. {
  68. builder.ToTable("Genres", CatalogContext.DEFAULT_SCHEMA);
  69. builder.HasKey(x => x.GenreId);
  70. builder.Property(x => x.GenreId);
  71. builder.Property(x => x.GenreDescription)
  72. .IsRequired()
  73. .HasMaxLength(1000);
  74.  
  75. builder.HasMany(navigation => navigation.Items);
  76.  
  77. }
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement