fluent api - Map table to another table with same key

0

I need to know how to map in the fluent API the following item

public class ImportacaoHeader
{
    public string IdImportacao { get; set; }
    public DateTime dataInicio { get; set; }
 }

public class ImportacaoLog
{
    public string IdImportacao { get; set; }
    public string CodigoCampo { get; set; }
}
The problem is as follows, ImportId in the ImportHeader table is key and ImportId is a key in ImportLog, since for each importheader I have several occurrences in ImportLog and the relationship between the two tables is the same key field in the two before of anything else I notice that I can not change the tables they already exist or I would have already done and solved, how can I map this in fluent api, how would the syntax look, and what would I have to change in the classes above?

    
asked by anonymous 23.01.2018 / 14:39

1 answer

0

See the example of how you can do it.

public class ImportacaoHeader
{
    public string IdImportacao { get; set; }
    public DateTime dataInicio { get; set; }
    public ICollection<ImportacaoLog> Importacao { get; set; }
}

public class ImportacaoLog
{
    public string IdImportacao { get; set; }
    public string CodigoCampo { get; set; }
    public ImportacaoHeader ImportacaoHeader { get; set; }
}

public class SeuContext : DbContext
{
    public DbSet<ImportacaoLog> ImportacaoLog { get; set; }
    public DbSet<GrImportacaoHeaderade> ImportacaoHeader { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // configures one-to-many relationship
        modelBuilder.Entity<ImportacaoLog>()
            .HasRequired<ImportacaoHeader>(s => s.ImportacaoHeader)
            .WithMany(g => g.ImportacaoLog)
            .Map(x => x.MapKey("IdImportacao "));  // Use o id da FK identica ao que esta no banco        
    }    
}

See how to map the fields with the same name as the one in the here .

Example;

Table name;

 modelBuilder.Entity<ImportacaoLog>().ToTable("XXXYYYZZZ"); // XXXYYYZZZ Nome que esta no banco

properties;

//Configure Column
        modelBuilder.Entity<ImportacaoLog>()
                    .Property(p => p.DateAnivesario)
                    .HasColumnName("DT_Anive") // DT_Anive o que esta no banco
                    .HasColumnOrder(3)
                    .HasColumnType("datetime2");

Reference 1

Reference 2

    
23.01.2018 / 15:51