autobus-api_old/AutobusApi.Infrastructure/Data/Configurations/AddressConfiguration.cs

64 lines
1.8 KiB
C#

using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NetTopologySuite.Geometries;
namespace AutobusApi.Infrastructure.Data.Configurations;
public class AddressConfiguration : EntityBaseConfiguration<Address>
{
public override void Configure(EntityTypeBuilder<Address> builder)
{
base.Configure(builder);
builder
.ToTable("addresses")
.HasKey(e => e.Id);
builder
.Property(a => a.Name)
.HasColumnName("name")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(a => a.VehicleType)
.HasColumnName("vehicle_type")
.HasColumnType("varchar(16)")
.HasConversion(
t => t.ToString(),
s => (VehicleType) Enum.Parse(typeof(VehicleType), s)
)
.IsRequired();
builder
.Property(a => a.Location)
.HasColumnName("location")
.HasColumnType("geography(point)")
.HasConversion(
l => new Point(l.Latitude, l.Longitude),
p => new Entities.Coordinates(p.X, p.Y)
)
.IsRequired();
builder
.Property(a => a.CityId)
.HasColumnName("city_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(a => a.City)
.WithMany(c => c.Addresses)
.HasForeignKey(a => a.CityId)
.HasConstraintName("fk_addresses_city_id")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(a => a.RouteAddresses)
.WithOne(ra => ra.Address)
.OnDelete(DeleteBehavior.Cascade);
}
}