在实体框架上保存的一对多问题

问题描述 投票:0回答:0

我有两个表,所以我创建了另一个表来创建多对多关系 Table relationships

这个想法是根据出发地和目的地来计算他的航班的旅程。从一个地方到另一个地方,可能需要多次飞行。

到目前为止,我做得很好,我有航班和旅程的大部分数据,但我无法正确填写多对多关系,我试过这种方式:

var FlightsRaw = await Services.GetSearchFlight();
            if (FlightsRaw != null)
            {
                Flights = new List<SearchFlightsResponse>(FlightsRaw);
                List<SearchFlightsResponse> route = Flights.Where(f => f.DepartureStation == origin).ToList();

                var SumPrice = 0.0;

                for (int i = 0; i < route.Count; i++)
                {
                    NewOrigin.Add(new Flight
                    {
                        FlightOrigin = Flights[i].DepartureStation,
                        FlightDestination = Flights[i].ArrivalStation,
                        FlightPrice = Flights[i].Price,
                        Transport = new Transport()
                        {
                            FlightCarrier = Flights[i].FlightCarrier,
                            FlightNumber = Flights[i].FlightNumber
                        }
                    });
                }


                for (int i = 0; i < Flights.Count; i++)
                {
                    if (Flights[i].ArrivalStation == destination)
                    {
                        RouteOrigin = Flights[i].DepartureStation;

                        for (int J = 0; J < NewOrigin.Count; J++)
                        {
                            if (NewOrigin[J].FlightDestination == RouteOrigin)
                            {
                                flights.Add(new Flight
                                {
                                    FlightOrigin = Flights[J].DepartureStation,
                                    FlightDestination = Flights[J].ArrivalStation,
                                    FlightPrice = Flights[J].Price,
                                    Transport = new Transport()
                                    {
                                        FlightCarrier = Flights[J].FlightCarrier,
                                        FlightNumber = Flights[J].FlightNumber
                                    }
                                });
                                SumPrice += Flights[J].Price;

                            }
                        }
                        flights.Add(new Flight
                        {
                            FlightOrigin = Flights[i].DepartureStation,
                            FlightDestination = Flights[i].ArrivalStation,
                            FlightPrice = Flights[i].Price,
                            Transport = new Transport()
                            {
                                FlightCarrier = Flights[i].FlightCarrier,
                                FlightNumber = Flights[i].FlightNumber
                            }
                        });
                        SumPrice += Flights[i].Price;

                    }
                }

                **var response = new Journey()**
                {
                    JourneyOrigin = origin,
                    JourneyDestination = destination,
                    JourneyPrice = SumPrice,
                    JourneyFlights = (ICollection<JourneyFlight>)flights

                };

但它给了我:

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[Domain.Flight]' to type 'System.Collections.Generic.ICollection`1[Domain.JourneyFlight]'.
   at NewShore.Controllers.SearchFlightsController.Get(String origin, String destination

在线:

var response = new Journey()

模特:

public class Flight
{
        public int FlightId { get; set; }
        public string? FlightOrigin { get; set; }
        public string? FlightDestination { get; set; }
        public double? FlightPrice { get; set; }
        public virtual ICollection<JourneyFlight> JourneyFlights { get; set; }

        public int TransportId { get; set; }
        public Transport? Transport { get; set; }
}

public class Journey
{
        public int JourneyId { get; set; }
        public string JourneyOrigin { get; set; }
        public string JourneyDestination { get; set; }
        public double JourneyPrice { get; set; }

        public virtual ICollection<JourneyFlight> JourneyFlights { get; set; }
}

public class JourneyFlight
{
        public int JourneyFlightId { get; set; }
        public int JourneyId { get; set; }
        public Journey? Journey { get; set; }
        public int FlightId { get; set; }
        public Flight? Flight { get; set; }
}

public class Transport
{
        public int TransportId { get; set; }
        public string? FlightCarrier { get; set; }
        public string? FlightNumber { get; set; }
        public virtual ICollection<Flight> Flights { get; set; }
}

        public DbSet<Flight> Flight { get; set; }
        public DbSet<Journey> Journey { get; set; }
        public DbSet<Transport> Transport { get; set; }
        public DbSet<JourneyFlight> JourneyFlight { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Flight>().HasKey(m => m.FlightId);

            modelBuilder.Entity<Journey>().HasKey(m => m.JourneyId);

            modelBuilder.Entity<Transport>().HasKey(m => m.TransportId);

            modelBuilder.Entity<JourneyFlight>().HasKey(m => m.JourneyFlightId);

            modelBuilder.Entity<JourneyFlight>()
          .HasOne(p => p.Flight)
          .WithMany(b => b.JourneyFlights)
          .HasForeignKey(t => t.FlightId);

            modelBuilder.Entity<JourneyFlight>()
           .HasOne(p => p.Journey)
           .WithMany(b => b.JourneyFlights)
           .HasForeignKey(t => t.JourneyId);

            modelBuilder.Entity<Flight>()
           .HasOne(p => p.Transport)
           .WithMany(b => b.Flights)
           .HasForeignKey(t => t.TransportId);
        }

有什么建议吗?

var response = new Journey()**
{
                    JourneyOrigin = origin,
                    JourneyDestination = destination,
                    JourneyPrice = SumPrice,
                    JourneyFlights = (ICollection<JourneyFlight>)flights

};

多对多的关系。

entity-framework .net-core table-relationships
© www.soinside.com 2019 - 2024. All rights reserved.