How to make the Mapper module in EF?

1

How to make the Mapper module in EmptyFramework 1 for n in two two tables?

Tabela 1

- clienteID          
- nome              
- endereçoID
Tabela 2    

- EndereçoID       
- rua
- bairro      
- cidade
    
asked by anonymous 04.07.2017 / 14:40

1 answer

2

I understood your relationship differently, it would not be aggregation in the Entity Framework , if yes that way :

public class Cliente
{
    public int ClienteId { get; set; }
    public string Nome { get; set; }        
    public virtual Endereco Endereco { get; set; }
}
public class Endereco
{
    public int ClienteId { get; set; }
    public string Rua { get; set; }
    public string Bairro { get; set; }
    public string Cidade { get; set; }  
    public virtual Cliente Cliente { get; set; }
}

Aggregation relationship settings:

public class ClienteConfiguration : 
        System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Cliente>
{
    public ClienteConfiguration()
        : this("dbo")
    {
    }

    public ClienteConfiguration(string schema)
    {
        ToTable("Cliente", schema);
        HasKey(x => x.ClienteId);

        Property(x => x.ClienteId)
            .HasColumnName("ClienteID")
            .HasColumnType("int")
            .IsRequired()
            .HasDatabaseGeneratedOption(System
                    .ComponentModel
                    .DataAnnotations
                    .Schema
                    .DatabaseGeneratedOption
                    .Identity);         
        Property(x => x.Nome)
            .HasColumnName("Nome")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);
    }
}
public class EnderecoConfiguration : 
        System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Endereco>
{
    public EnderecoConfiguration()
        : this("dbo")
    {
    }

    public EnderecoConfiguration(string schema)
    {
        ToTable("Endereco", schema);
        HasKey(x => x.ClienteId);

        Property(x => x.ClienteId)
            .HasColumnName("ClienteID")
            .HasColumnType("int")
            .IsRequired()
            .HasDatabaseGeneratedOption(System
                    .ComponentModel
                    .DataAnnotations
                    .Schema
                    .DatabaseGeneratedOption,
                    .None);
        Property(x => x.Rua)
            .HasColumnName("Rua")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);
        Property(x => x.Bairro)
            .HasColumnName("Bairro")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);
        Property(x => x.Cidade)
            .HasColumnName("Cidade")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);

        HasRequired(a => a.Cliente)
            .WithOptional(b => b.Endereco)
            .WillCascadeOnDelete(true);
    }
}

Now if it is 1 Client has multiple Addresses then it changes completely:

public class Cliente
{
    public int ClienteId { get; set; }
    public string Nome { get; set; }
    public virtual ICollection<Endereco> Endereco { get; set; }
    public Cliente()
    {
        Endereco = new System.Collections.Generic.List<Endereco>();
    }
}
public class Endereco
{
    public int EnderecoId { get; set; }
    public int ClienteId { get; set; }
    public string Rua { get; set; }
    public string Bairro { get; set; }
    public string Cidade { get; set; }
    public virtual Cliente Cliente { get; set; }
}

Relationship Setting (1: N)

public class ClienteConfiguration : 
    System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Cliente>
{
    public ClienteConfiguration()
        : this("dbo")
    {
    }

    public ClienteConfiguration(string schema)
    {
        ToTable("Cliente", schema);
        HasKey(x => x.ClienteId);

        Property(x => x.ClienteId)
            .HasColumnName("ClienteID")
            .HasColumnType("int")
            .IsRequired()
            .HasDatabaseGeneratedOption(System
                    .ComponentModel
                    .DataAnnotations
                    .Schema
                    .DatabaseGeneratedOption
                    .Identity);
        Property(x => x.Nome)
            .HasColumnName("Nome")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);
    }
}
public class EnderecoConfiguration : 
    System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Endereco>
{
    public EnderecoConfiguration()
        : this("dbo")
    {
    }

    public EnderecoConfiguration(string schema)
    {
        ToTable("Endereco", schema);
        HasKey(x => x.EnderecoId);

        Property(x => x.EnderecoId)
            .HasColumnName("EnderecoID")
            .HasColumnType("int")
            .IsRequired()
            .HasDatabaseGeneratedOption(System
                    .ComponentModel
                    .DataAnnotations
                    .Schema
                    .DatabaseGeneratedOption
                    .Identity);
        Property(x => x.ClienteId)
            .HasColumnName("ClienteID")
            .HasColumnType("int")
            .IsRequired();
        Property(x => x.Rua)
            .HasColumnName("Rua")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);
        Property(x => x.Bairro)
            .HasColumnName("Bairro")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);
        Property(x => x.Cidade)
            .HasColumnName("Cidade")
            .HasColumnType("varchar")
            .IsRequired()
            .IsUnicode(false)
            .HasMaxLength(50);

        HasRequired(a => a.Cliente)
            .WithMany(b => b.Endereco)
            .HasForeignKey(c => c.ClienteId)
            .WillCascadeOnDelete(false);
    }
}

One of the two may be what you are looking for, what is really worth is to understand which you often need to do all the fields in the table Cliente would be ideal, but the division depending on the cases can also serve you very well, now if a client can have multiple addresses use the second option.

Setting DbContext for application :

public class MyDbContext: System.Data.Entity.DbContext
{
    public DbSet<Cliente> Cliente { get; set; }
    public DbSet<Endereco> Endereco { get; set; }
    public MyDbContext()
        : base("Name=DatabaseEntities")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new ClienteConfiguration());
        modelBuilder.Configurations.Add(new EnderecoConfiguration());
    }
}

References:

04.07.2017 / 15:52