If the database was already created, I would recommend using Database First to generate the entities and the mapping automatically.
Here you can find some documentation of Database First:
link
On the other hand, if you want to use Code First, you would have to map each of the tables.
With regard to mapping, this is manual and requires too much effort since you would have to do table by table.
Below is an example of mapping the user table.
public class UsuarioMapping : EntityTypeConfiguration<Usuario>
{
public UsuarioMapping()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("id");
Property(x => x.Nombre).HasColumnName("nombre");
Property(x => x.ApellidoPaterno).HasColumnName("apellidoPaterno");
Property(x => x.ApellidoMaterno).HasColumnName("apellidoMaterno");
Property(x => x.Correo).HasColumnName("correo");
Property(x => x.NombreUsuario).HasColumnName("usuario");
Property(x => x.Contrasena).HasColumnName("contrasena");
Property(x => x.Estatus).HasColumnName("estatus");
ToTable("tblUsuarios");
}
}
The context would be as follows:
public class MainContext : DbContext
{
public DbSet<Usuario> Usuario { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
try
{
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType
&& type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstancie = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstancie);
}
base.OnModelCreating(modelBuilder);
}
catch (Exception e)
{
//handle
}
}
}