Entity framework Client For Sale

Asked

Viewed 41 times

0

I’m having doubts about how to build the relationship between the Client and Sale classes, taking a look at the microsoft website, I was left with doubt if the class should have the navigation for another.

I want the customer to have a list of Sales and that Sales has a list of Itemvenda.

Should I place a property to do the Sale navigation for customer? 'Cause I see that Venda doesn’t need to guard the customer, but the customer keeps his sale.

public class Cliente
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Sobrenome { get; set; }
    public string Email { get; set; }
    public DateTime DataCadastro { get; set; }
    public bool Ativo { get; set; }

    public ICollection<Venda> Vendas { get; }
}

public class Venda
{   
    public int Id { get; private set; }
    public DateTime Data { get; private set; }
    public Cliente Cliente { get; set; }
    public int ClienteId { get; set; }

    public ICollection<ItemVenda> ItemVenda { get; }
}


public class ItemVenda
{
    public int Id { get; set; }
    public int IdProduto { get; set; }
    public int IdVenda { get; set; }
    public int Quantidade { get; set; }
    public double Valor { get; set; }

    public virtual Produto Produto { get; set; }
    public ICollection<Produto> Produtos { get; private set; }

}

What is the right way to set them up in Entitytypeconfiguration

public class ConfiguracoesdeCliente : EntityTypeConfiguration<Cliente>
{
    public ConfiguracoesdeCliente()
    {
        HasKey(c => c.Id);

        Property(c => c.Nome)
            .IsRequired()
            .HasMaxLength(150);

        Property(c => c.Sobrenome)
            .IsRequired()
            .HasMaxLength(150);

        Property(c => c.Email)
            .IsRequired();

        //HasMany(p => p.Vendas);

        HasOptional(p => p.Vendas)
            .WithMany()
            .HasForeignKey(p => p.Vendas);

    }
}

public class ConfiguracoesdeVenda : EntityTypeConfiguration<Venda>
{
    public ConfiguracoesdeVenda()
    {
        HasKey(v => v.Id);

        Property(v => v.Data)
            .IsRequired();

        HasMany(v => v.ItemVenda)
            .WithMany();
    }
}

public class ConfiguracoesdeItemVenda : EntityTypeConfiguration<ItemVenda>
{
    public ConfiguracoesdeItemVenda()
    {
        HasKey(p => p.Id);

        Property(p => p.VendaId)
            .IsRequired();

        Property(p => p.ProdutoId)
            .IsRequired();

        Property(p => p.Quantidade)
            .IsRequired();

        Property(p => p.Valor)
            .IsRequired();

        HasRequired(p => p.Venda)
            .WithMany(p => p.ItemVenda);

        HasRequired(p => p.Produto);

        //HasRequired(iv => iv.Venda);
    }
}
  • What version of the Entity Framework?

  • 1

    The version is 6.0.0.0

1 answer

1


This depends a lot on how you want to manipulate your data.

In general we create a list in the Customer with its sales and a Customer for sale to facilitate navigation if you are with the sale and want to fetch the customer from that sale.

Your property public int ClienteId { get; set; } could be accompanied with a client’s own property written as virtual which causes the customer to be already loaded depending on the settings of the Configuration.LazyLoadingEnabled of EF.

In the RU documentation has step by step how you can assemble your logic according to what you need.

  • 1

    Then in the Sale class I place the Client and the Clienteid int?

  • 1

    If I ride like you said, as I set up the 2 classes with each other

Browser other questions tagged

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