Fluent API Modeling with Reference Table

1

I would like to know how to perform the modeling of a particular entity, where its reference value belongs to another table. This reference value would be a domain table that has its ID's for credit cards, since my user can register that their service accepts multiple credit card flags such as Visa, Master, etc.

Below is an example of my entities:

public class CartaoCreditoUsuario 
{
    public string Id { get; set; }
    public int CartaoCreditoId { get; set; }
    public virtual Usuario.Usuario Usuario { get; set; }        
}

public class Usuario
{
    public Usuario()
    {
        Id = Guid.NewGuid().ToString();
        Enderecos = new List<Endereco>();
    }

    public string Id { get; set; }
    public virtual string Email { get; set; }
    public virtual bool ConfirmaçãoEmail{ get; set; }
    public virtual ICollection<CartaoCreditoUsuario> CartaoCreditoUsuario { get; set; }
       ....

}

public class CartaoCredito
{
    public int CartaoCreditoId { get; set; }
    public string DescricaoCartaoCredito { get; set; }
}

I would like to know what my modelBuilder.Configurations would look like for the entities mentioned above.

Thank you in advance.

    
asked by anonymous 20.11.2016 / 00:09

1 answer

1

If I understand what you want, it looks like this:

modelBuilder.Entity<Usuario>() 
    .HasMany(t => t.CartoesCredito) 
    .WithMany(t => t.Usuarios) 
    .Map(m => 
    { 
        m.ToTable("CartoesCreditoUsuarios"); 
        m.MapLeftKey("Id"); 
        m.MapRightKey("CartaoCreditoId"); 
    });

In this case, the entities look like this:

public class Usuario
{
    public Usuario()
    {
        Id = Guid.NewGuid().ToString();
        Enderecos = new List<Endereco>();
    }

    public string Id { get; set; }
    public virtual string Email { get; set; }
    public virtual bool ConfirmaçãoEmail{ get; set; }
    public virtual ICollection<CartaoCredito> CartoesCredito { get; set; }
       ....

}

public class CartaoCredito
{
    public int CartaoCreditoId { get; set; }
    public string DescricaoCartaoCredito { get; set; }

    public virtual ICollection<Usuario> Usuarios { get; set; }    
}

Note that because the Fluent API controls everything, you have no control over the associative entity. To map manually, you will have to give up the Fluent API and map as follows:

public class CartaoCreditoUsuario 
{
    public int CartaoCreditoUsuarioId { get; set; }
    public string UsuarioId { get; set; }
    public int CartaoCreditoId { get; set; }

    public virtual Usuario Usuario { get; set; }
    public virtual CartaoCredito CartaoCredito { get; set; }
}

public class Usuario
{    
    public string Id { get; set; }
    public virtual string Email { get; set; }
    public virtual bool ConfirmacaoEmail{ get; set; }
    public virtual ICollection<CartaoCreditoUsuario> CartoesCreditoUsuario { get; set; }
       ....

}

public class CartaoCredito
{
    public int CartaoCreditoId { get; set; }
    public string DescricaoCartaoCredito { get; set; }

    public virtual ICollection<CartaoCreditoUsuario> UsuariosCartaoCredito { get; set; }

}

It takes no more than simple class declaration to work.

    
21.02.2017 / 00:17