public class Station
{
[Key]
public int StationId { get; set; }
[Required]
[Column(TypeName = "VARCHAR")]
[StringLength(100)]
public string TerminalName { get; set; }
[Required]
[Column(TypeName = "VARCHAR")]
[StringLength(100)]
public string City { get; set; }
[Required]
[Column(TypeName = "VARCHAR")]
[StringLength(100)]
public string Province { get; set; }
//Add This two Lists create many to many
public List<Mapped_Stations> Origins { get; set; }
public List<Mapped_Stations> Destinations { get; set; }
}
}
public class Mapped_Stations
{
public int OriginId { get; set; }
public int DestinationId { get; set; }
public Station Origin { get; set; }
public Station Destination { get; set; }
}
public class Mapped_SatationsMap : IEntityTypeConfiguration<Mapped_Stations>
{
public void Configure(EntityTypeBuilder<Mapped_Stations> builder)
{
builder.HasKey(t => new { t.OriginId, t.DestinationId });
builder
.HasOne(t => t.Origin)
.WithMany(p => p.Origins)
.HasForeignKey(f => f.OriginId);
builder
.HasOne(t => t.Destination)
.WithMany(p => p.Destinations)
.HasForeignKey(f => f.DestinationId).OnDelete(DeleteBehavior.Restrict);
}
}
public class Db:DbContext
{
public Db() : base() { }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(@"Server=(local);Database=Many-to-many;Trusted_Connection=True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new Mapped_SatationsMap());
}
DbSet<Mapped_Stations> Mapped_Stations { get; set; }
DbSet<Station> stations { get; set; }
}