using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repositorio.Entidades;
using System.Data.Entity;
using System.Data.Entity.Validation;


namespace Repositorio.DAL.Contexto
{
    public class BancoContexto : DbContext
    {
        public BancoContexto() : base("ConnDB") { }

         public override int SaveChanges()
    {
        try
        {
            return base.SaveChanges();
        }
        catch (DbEntityValidationException ex)
        {
            // Retrieve the error messages as a list of strings.
            var errorMessages = ex.EntityValidationErrors
                    .SelectMany(x => x.ValidationErrors)
                    .Select(x => x.ErrorMessage);
    
            // Join the list to a single string.
            var fullErrorMessage = string.Join("; ", errorMessages);
    
            // Combine the original exception message with the new one.
            var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
    
            // Throw a new DbEntityValidationException with the improved exception message.
            throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
        }
    }





        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Local>().ToTable("TbCTGLocal");
            modelBuilder.Entity<TipoEvento>().ToTable("TbCTGTipoEventos");
            modelBuilder.Entity<Treinador>().ToTable("TbCTGTreinador");
            modelBuilder.Entity<Evento>().ToTable("TbCTGEventos");
            modelBuilder.Entity<EventoTreinador>().ToTable("TbCTGEventoTreinador");
            modelBuilder.Entity<Participante>().ToTable("TbCTGParticipante");

            /* modelBuilder.Entity<Evento>()
                            .HasMany<Treinador>(s => s.Treinadores)
                   .WithMany(e => e.Eventos)
                   .Map(cs =>
                            {
                                cs.MapLeftKey("TreinadorID");
                                cs.MapRightKey("EventoId");
                                cs.ToTable("tbCTGEventoTreinador");
                                
                            }); 

            */
            
        }
        

        }

    
}
