Fluent API modeling with reference table

Asked

Viewed 59 times

1

I would like to know how I perform the modeling of a given entity, where its reference value belongs to another table. This benchmark would be a domain table that has your credit card id s, because my user will be able to register that your service accepts several credit card flags like 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 wish I knew how it would look mine modelBuilder.Configurations referring to the entities cited above.

Thank you in advance.

1 answer

1

If I understand what you want, that’s it:

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 are 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 since the Fluent API controls everything, you have no control over the associative entity. To manually map, you will need 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 a simple class declaration to work.

Browser other questions tagged

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