Entity Framework approach code firts

0

I want to use EF the Code Firts environment, I see on the web that there are two ways a simple one that only creates the DbSet runs and the database is created.

public class CompanyContext : DbContext
{
    public CompanyContext() : base("CompanyDatabase") { }

    public DbSet<Collaborator> Collaborators { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Manager> Managers { get; set; }
}

But I've also seen that a class is created with the terminology at the end Map as ManagerMap.

public SeccionMap()
    {
        ToTable("Secciones");
        HasKey(c => c.SeccionId);

    }

Both create the database, but I think the second option is the most appropriate ?, You see that you have more control of what you want to do.

Someone can pass me a tutorial or link of the second option to use the code firts approach.

    
asked by Pedro Ávila 31.03.2016 в 03:32
source

1 answer

0

Actually both options that you mention can be applied according to the degree of adjustment that you need about mapping

If you define classes based on convenciones you may not need any class Map

Code First Conventions

Now if there is any aspect in the definition of the mapping that requires an adjustment there is when you create the class Map to define that adjustment

[Entity Framework] [Code First] Create simple entity

In the article I explain it in more detail, but remember that the mapping is generated in a class that hurts from EntityTypeConfiguration

public class EtapaMap : EntityTypeConfiguration<Etapa>
{
    ToTable("Etapas");
    HasKey(c => c.EtapaId);
    //resto mapping
}

public class CompanyContext : DbContext
{
    public CompanyContext() : base("CompanyDatabase") { }

    public DbSet<Collaborator> Collaborators { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Manager> Managers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Configurations.Add(new EtapaMap());

         base.OnModelCreating(modelBuilder);
    }
}

Note the OnModelCreating as the configuration is defined

    
answered by 31.03.2016 / 03:53
source