Fluent api - Map table to another table with the same key

Asked

Viewed 61 times

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 the following, Idimportacao in the table Importacaoheader is key and Idimportacao tb is key in importacaoLog, Being that for each importacaoheader I have several occurrences in Importacaolog and the relationship between the two tables is the same key field in the two, first of all I warn you that I can not change the tables they already exist otherwise would have done and solved, how can I map this in the Fluent api, what the syntax would look like, and what I would have to change in the above classes?

  • If the field IdImportacao on the table ImportacaoLog is key primary, it is impossible to have multiple occurrences for the same ImportacaoHeader. Or she’s a Foreign Key (then it would have some other field being primary key), or it is a composite primary key, in which case the primary key could be composed by the IdImportacao and by CodigoCampo, that is, it could have several occurrences for the same ImportacaoHeader, but only one for each CodigoCampo. Which of the two situations is yours?

1 answer

0


See the example of how you can do.

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 fields with the same name in the database here.

Example;

Table name;

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

estates;

//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

  • So I managed to resolve I will post here later

Browser other questions tagged

You are not signed in. Login or sign up in order to post.