autobus-api_old/AutobusApi.Persistence/Contexts/Configurations/RegionConfiguration.cs

42 lines
1.1 KiB
C#

using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class RegionConfiguration : EntityBaseConfiguration<Region>
{
public override void Configure(EntityTypeBuilder<Region> builder)
{
base.Configure(builder);
builder
.ToTable("regions")
.HasKey(e => e.Id);
builder
.Property(r => r.Name)
.HasColumnName("name")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(r => r.CountryId)
.HasColumnName("country_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(r => r.Country)
.WithMany(c => c.Regions)
.HasForeignKey(r => r.CountryId)
.HasConstraintName("fk_regions_coutries_countryId")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(r => r.Cities)
.WithOne(c => c.Region)
.OnDelete(DeleteBehavior.Cascade);
}
}