How to make the Mapper module in EF?

Asked

Viewed 49 times

1

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

Tabela 1

- clienteID          
- nome              
- endereçoID

Tabela 2    

- EndereçoID       
- rua
- bairro      
- cidade
  • A Customer can have an address or several? sorry it got weird this? I even put an answer but, I can change !

  • only one is that I cannot use the real names of the fields

  • Bruno then in my answer is the first part, take a look and suit your real way there!

  • Made some exceptions error here, I’ll structure better with the code I already have and put here.

  • When you put the doubt you should pay attention to the following: that it reflects to your model and that you can understand, if you can not put name fields becomes complicated, but, try to resemble what was made. Put the real model in your question, it’s silly not to put!

  • 1

    It’s what I’ve tried the most is much more complex than I imagine it leads to several calls that are already ready

Show 2 more comments

1 answer

2


I understood your relationship differently, it wouldn’t be aggregation on Entity Framework, if yes it is so:

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; }
}

Settings of the aggregation relationship:

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; }
}

Relation Settings (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 can be what you are looking for, what is really worth understanding is which one 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.


Configuring the 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:

Browser other questions tagged

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