Generating a schema for the Artist and Genre entities

The preceding process can also be applied to the Artist and Genre entities. The following code shows the definitions of the two entities in the Catalog.Domain.Entities namespace:

using System.Collections.Generic;

namespace Catalog.Domain.Entities
{
//Artist.cs
public class Artist
{
public Guid ArtistId { get; set; }
public string ArtistName { get; set; }
public ICollection<Item> Items { get; set; }
}

//Genre.cs
public class Genre
{
public Guid GenreId { get; set; }
public string GenreDescription { get; set; }
public ICollection<Item> Items { get; set; }
}
}

Consequently, we can add two files to the Catalog.Infrastructure project as follows:

 //SchemaDefinitions/ArtistEntitySchemaConfiguration.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Catalog.Domain.Entities;

namespace Catalog.Infrastructure.SchemaDefinitions
{
public class ArtistEntitySchemaConfiguration :
IEntityTypeConfiguration<Artist>
{
public void Configure(EntityTypeBuilder<Artist> builder)
{
builder.ToTable("Artists", CatalogContext.DEFAULT_SCHEMA);
builder.HasKey(k => k.ArtistId);

builder.Property(p => p.ArtistId);

builder.Property(p => p.ArtistName)
.IsRequired()
.HasMaxLength(200);
}
}
}

The GenreEntitySchemaConfiguration.cs file looks as follows:

 //SchemaDefinitions/GenreEntitySchemaConfiguration.cs
using Catalog.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Catalog.Infrastructure.SchemaDefinitions
{
public class GenreEntitySchemaConfiguration :
IEntityTypeConfiguration<Genre>
{
public void Configure(EntityTypeBuilder<Genre> builder)
{
builder.ToTable("Genres", CatalogContext.DEFAULT_SCHEMA);
builder.HasKey(k => k.GenreId);

builder.Property(p => p.GenreId);

builder.Property(p => p.GenreDescription)
.IsRequired()
.HasMaxLength(1000);
}
}
}

Both GenreEntitySchemaConfiguration and ArtistEntitySchemaConfiguration define the keys for our tables using the HasKey method. As we have already discussed, they use the same fluent approach applied to the ItemEntitySchemaConfiguration class defined previously. Also, we need to include GenreEntitySchemaConfiguration and ArtistEntitySchemaConfiguration in the OnModelCreating method of the CatalogContext class:


public class CatalogContext : DbContext, IUnitOfWork
{
...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ItemEntitySchemaDefinition());
modelBuilder.ApplyConfiguration(new GenreEntitySchemaConfiguration());
modelBuilder.ApplyConfiguration(new ArtistEntitySchemaConfiguration());

base.OnModelCreating(modelBuilder);
}
...
}

I've omitted the full definition of the CatalogContext class for brevity. The significant change is the extension of the OnModelCreating method by applying the configuration for the GenreEntitySchemaConfiguration and ArtistEntitySchemaConfiguration classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.51.117