@Jhon Hernandez, Welcome to Microsoft Q&A, If you want to save many to many relationship in ef core, I recommend that you use the code first to generate the models and define some relationship in OnModelCreating event.
Here is a code example you could refer to.
Model and Dbcontext:
public class JourneyFlight
{
public int JourneyFlightId { get; set; }
public int JourneyId { get; set; }
public int FlightId { get; set; }
public Journey? journey { get; set; }
public Flight? flight { get; set; }
}
public class Journey
{
public int JourneyId { get; set; }
public double JourneyPrice { get; set; }
public ICollection<JourneyFlight>? journeyFlights { get; set; }
}
public class Flight
{
public int FlightId { get; set; }
public double FlightPrice { get; set;}
public int TransportId { get; set; }
public Transport? transport { get; set; }
public ICollection<JourneyFlight>? journeyFlights { get; set; }
}
public class Transport
{
public int TransportId { get; set; }
public int TransportFlightNumber { get; set; }
public ICollection<Flight>? flights;
}
public class MyContext:DbContext
{
public DbSet<JourneyFlight> journeyFlights { get; set; }
public DbSet<Journey> journeys { get; set; }
public DbSet<Flight> flights { get; set; }
public DbSet<Transport> transports { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connstr");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<JourneyFlight>().HasKey(m => new { m.JourneyId, m.FlightId });
modelBuilder.Entity<JourneyFlight>().HasOne(m => m.journey).WithMany(m => m.journeyFlights).HasForeignKey(m=>m.JourneyId);
modelBuilder.Entity<JourneyFlight>().HasOne(m => m.flight).WithMany(m => m.journeyFlights).HasForeignKey(m=>m.FlightId);
modelBuilder.Entity<Flight>().HasOne(m => m.transport).WithMany(m => m.flights).HasForeignKey(m => m.TransportId);
}
}
How to add data in main method:
MyContext context=new MyContext();
JourneyFlight journeyFlight1=new JourneyFlight();
JourneyFlight journeyFlight2 = new JourneyFlight();
var j1=new Journey();
j1.JourneyPrice = 100;
var j2=new Journey();
j2.JourneyPrice = 200;
var f1=new Flight();
f1.FlightPrice = 50;
var f2=new Flight();
f2.FlightPrice = 70;
journeyFlight1.journey = j1;
journeyFlight1.flight = f1;
journeyFlight2.journey = j2;
journeyFlight2.flight = f2;
Transport transport = new Transport();
transport.flights = new List<Flight>
{
f1,
f2
};
transport.TransportFlightNumber = 1001;
context.journeyFlights.Add(journeyFlight1);
context.journeyFlights.Add(journeyFlight2);
context.transports.Add(transport);
context.SaveChanges();
Based on my test, I could get the following result in sql server:
Hope my code example could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.