Persistence using Fluent API

Asked

Viewed 83 times

4

How do I persist an address without having to pass on all customer information?

Below is my address client mapping:

HasMany(f => f.EnderecoList)
            .WithMany(e => e.ClienteList)
            .Map(me =>
            {
                me.MapLeftKey("ClienteId");
                me.MapRightKey("EnderecoId");
                me.ToTable("EnderecosCliente");
            });

2 answers

2

Ideally you do not use the Fluent API, rather an associative entity:

public class Cliente
{
    /* Propriedades do Cliente */

    public virtual EnderecoCliente EnderecoCliente { get; set; }
}

public class Endereco
{
    /* Propriedades do Endereco */

    public virtual EnderecoCliente EnderecoCliente { get; set; }
}

And then:

public class EnderecoCliente 
{
    [Key]
    public int EnderecoClienteId { get; set; }
    [Index("IUQ_EnderecoCliente_ClienteId_EnderecoId", IsUnique = true, Order = 1)]
    public int ClienteId { get; set; }
    [Index("IUQ_EnderecoCliente_ClienteId_EnderecoId", IsUnique = true, Order = 2)]
    public int EnderecoId { get; set; }

    public virtual Endereco Endereco { get; set; }
    public virtual Cliente Cliente { get; set; }
}

Defining:

var endereco = new Endereco 
{
    /* Defina aqui outras propriedades */
    EnderecoCliente = new EnderecoCliente {
        Cliente = contexto.Clientes.FirstOrDefault(c => c.ClienteId == 5)
    }
};

[Index], introduced in this form from the Entity Framework 6.1.0, ensures the uniqueness of the associative record. Additional validations may be required in the application to avoid strange key duplicity errors for the user.

  • as would be my model at the time of persistence of a new address , I would have a list of Addressecocliente, that’s it ?

  • Not a list. A single record.

  • I misplaced the example. Now it’s right.

  • but the question is persistence, suppose that at the moment I am registering addresses this client has 3 or 4 different addresses. How do I persist if the model only receives a record ?

  • So, see if it’s clearer now. I edited the answer.

  • just to understand I have a register that tells me the following, billing address, delivery address, home address, each in a different place , how to persist this ? Thank you @Ciganomorrisonmendez

  • So you create using that syntax that I put up there. The block of creating the association with the client is the same for the 4.

Show 2 more comments

1

The way you’re mapping it is a many-to-many relation... If you want a client to have multiple addresses try something like this:

    public class ClienteEntity
{
    [Key]
    public int Codigo { get; set; }
    public string Nome { get; set; }
    public virtual HashSet<EnderecoEntity> Enderecos { get; set; }
}

public class EnderecoEntity
{
    [Key]
    public int Codigo { get; set; }
    public string Logradouro { get; set; }

    public int? CodigoCliente { get; set; }
    public virtual ClienteEntity Cliente { get; set; }
}

public class ClienteContext:DbContext
{
    public DbSet<ClienteEntity> Clientes { get; set; }
    public DbSet<EnderecoEntity> Enderecos { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder
            .Entity<ClienteEntity>()
            .HasMany(t=>t.Enderecos)
            .WithOptional(c=>c.Cliente)
            .HasForeignKey(c=>c.CodigoCliente);
    }
}

With this it is possible to fill several addresses with a client and arrive from the address in the corresponding client...

Browser other questions tagged

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