aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.ServiceViewer/Data/AppDbContext.cs
blob: 50f079101adf12a5caec548bc88a496b7ab8d051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Costasdev.ServiceViewer.Data.Gtfs;
using Costasdev.ServiceViewer.Data.Gtfs.Enums;
using Microsoft.EntityFrameworkCore;

namespace Costasdev.ServiceViewer.Data;

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Route -> Agency
        modelBuilder.Entity<GtfsRoute>()
            .HasOne(r => r.Agency)
            .WithMany()
            .HasForeignKey(r => new { r.AgencyId, r.FeedId })
            .HasPrincipalKey(a => new { a.Id, a.FeedId });

        // Trip -> Route
        modelBuilder.Entity<GtfsTrip>()
            .HasOne(t => t.Route)
            .WithMany()
            .HasForeignKey(t => new { t.RouteId, t.FeedId })
            .HasPrincipalKey(a => new { a.Id, a.FeedId });

        // Relación StopTimes -> Trip
        modelBuilder.Entity<GtfsStopTime>()
            .HasOne(st => st.GtfsTrip)
            .WithMany()
            .HasForeignKey(st => new { st.TripId, st.FeedId })
            .HasPrincipalKey(a => new { a.Id, a.FeedId });

        // Relación StopTimes -> Stop
        modelBuilder.Entity<GtfsStopTime>()
            .HasOne(st => st.GtfsStop)
            .WithMany()
            .HasForeignKey(st => new { st.StopId, st.FeedId })
            .HasPrincipalKey(a => new { a.Id, a.FeedId });

        modelBuilder.Entity<GtfsTrip>()
            .Property(t => t.TripWheelchairAccessible)
            .HasDefaultValue(TripWheelchairAccessible.Empty);

        modelBuilder.Entity<GtfsTrip>()
            .Property(t => t.TripBikesAllowed)
            .HasDefaultValue(TripBikesAllowed.Empty);
    }

    public DbSet<GtfsAgency> Agencies { get; set; }
    public DbSet<GtfsCalendar> Calendars { get; set; }
    public DbSet<GtfsCalendarDate> CalendarDates { get; set; }
    public DbSet<GtfsRoute> Routes { get; set; }
    public DbSet<GtfsStop> Stops { get; set; }
    public DbSet<GtfsStopTime> StopTimes { get; set; }
    public DbSet<GtfsTrip> Trips { get; set; }
}