Inheritance Mapping Type per Type Asp.Net

1

In my project, I have a type of inheritance that I mapped by TPT. Looking at the documents and several examples I can not identify where I'm going wrong, because the mapping seems to be correct, class definitions, everything.

Problem: I have three classes: Person, Client and Broker. Client and Broker inherit from Person and so is in the mapping. When in the system I make any search using the DbSet of the subclasses, the query that the identity mounts is searching of the parent class, that is, if I made a search using the Client type, identity assembles a query from the superclass (Person) and not from the subclass (Client).

public abstract class Pessoa
{
    public Pessoa()
    {
    }

    public string Nome { get; set; }
    public string NumeroCpfCnpj { get; set; }
    public string NumeroRgIe { get; set; }
    public string FlagEstadoCivil { get; set; }
    public string Profissao { get; set; }
    public DateTime DataDeNascimento { get; set; }
    public string TelefoneFixo { get; set; }
    public string TelefoneCelular { get; set; }
    public string Fax { get; set; }
    public string Endereco { get; set; }
    public string Numero { get; set; }
    public string Complemento { get; set; }
    public string NomeMae { get; set; }
    public string NomePai { get; set; }
    public string NomeConjuge { get; set; }
    public DateTime DataNascimentoConjuge { get; set; }
    public DateTime DataCasamento { get; set; }
    public string Email { get; set; }
    public float RendaFamiliar { get; set; }
    public string Bairro { get; set; }
    public string CEP { get; set; }
    public int? CidadeId { get; set; }
    public string Observacoes { get; set; }
    public int RegimeCasamentoId { get; set; }
    public int EstadoCivilId { get; set; }
    public bool IsDeleted { get; set; }
    public DateTime DataEmissao { get; set; }
    public string OrgaoEmissor { get; set; }
    public string EstadoEmitente { get; set; }
    public Sexo Sexo { get; set; }
}

public class Cliente : Pessoa
{
    public Cliente()
    {
        FlagMauPagador = false;
        StatusCobrancaId = 1;
        FlagImpedirRealizarVenda = false;
        FlagImpedirGerarBoleto = false;
        FlagImpedirSairNoRelatorioCobranca = false;
    }

    public bool FlagMauPagador { get; set; }
    public int StatusCobrancaId { get; set; }
    public bool FlagImpedirRealizarVenda { get; set; }
    public bool FlagImpedirGerarBoleto { get; set; }
    public bool FlagImpedirSairNoRelatorioCobranca { get; set; }
}


public class Corretor : Pessoa
{
    public Corretor()
    {
        Produtos = new List<Produto>();
        Loteamentos = new List<Loteamento>();
    }

    public int CodCorretor { get; set; }
    public string NomeCorretor { get; set; }
    public string Creci { get; set; }
    public decimal ValorDescontoMaximo { get; set; }
    public int? IdParceiroVenda { get; set; }
    public bool flgAtivo { get; set; }
    public ICollection<Produto> Produtos { get; set; }
    public ICollection<Loteamento> Loteamentos { get; set; }
}


public class PessoaConfiguration : EntityTypeConfiguration<Pessoa>
{
    public PessoaConfiguration()
    {
        ToTable("pessoa");

        HasKey(c => c.Id);

        Property(c => c.Id)
            .HasColumnName("cod_pessoa")
            .IsRequired();

        [...]

     }
}

 public class CorretorConfiguration : EntityTypeConfiguration<Corretor>
{
    public CorretorConfiguration()
    {
        ToTable("corretor");

       [...]
    }
}


public class ClienteConfiguration : EntityTypeConfiguration<Cliente>
{
    public ClienteConfiguration()
    {
        ToTable("cliente");

          [...]
     }
}
    
asked by anonymous 26.04.2017 / 17:22

1 answer

0

Note: You have adjustments to make, in the abstract class Pessoa must have Id that is intended to generate the code identifier of the Person table and that will be used for write in the Client and Broker tables that also have a Id field, but it is not auto increment, inclusive in Corretor does not need to have the CodCorretor field is to remove

public abstract class Pessoa
{
    public Pessoa()
    {
    }
    public int Id { get; set; } // campo a ser inserido
    public string Nome { get; set; }
    public string NumeroCpfCnpj { get; set; }
    public string NumeroRgIe { get; set; }
    public string FlagEstadoCivil { get; set; }
    public string Profissao { get; set; }
    public DateTime DataDeNascimento { get; set; }
    public string TelefoneFixo { get; set; }
    public string TelefoneCelular { get; set; }
    public string Fax { get; set; }
    public string Endereco { get; set; }
    public string Numero { get; set; }
    public string Complemento { get; set; }
    public string NomeMae { get; set; }
    public string NomePai { get; set; }
    public string NomeConjuge { get; set; }
    public DateTime DataNascimentoConjuge { get; set; }
    public DateTime DataCasamento { get; set; }
    public string Email { get; set; }
    public float RendaFamiliar { get; set; }
    public string Bairro { get; set; }
    public string CEP { get; set; }
    public int? CidadeId { get; set; }
    public string Observacoes { get; set; }
    public int RegimeCasamentoId { get; set; }
    public int EstadoCivilId { get; set; }
    public bool IsDeleted { get; set; }
    public DateTime DataEmissao { get; set; }
    public string OrgaoEmissor { get; set; }
    public string EstadoEmitente { get; set; }
    public Sexo Sexo { get; set; }
}

In classes Corretor and Cliente need not have this field Id , only in tables as explained in the note. >

Reconfigure the part of the mapping of the 3 classes, referring to the changes

To have the expected effect you should now create a class inheriting from DbContext with the following code:

public class Database : DbContext
{
    public Database()
        :base("connectionString") { }      

    public DbSet<Pessoa> Pessoa {get;set;}
}

To insert data create classes

Database db = new Database();

Cliente c = new Cliente();
c. ... // aqui sãos os campos e seus valores

db.Pessoa.Add(c);
db.SaveChanges();

and

Corretor co = new Corretor();
co. ... // aqui sãos os campos e seus valores

db.Pessoa.Add(co);
db.SaveChanges();

To retrieve the information you must use the OfType<> offering the type matches for recovery (which in this case is Cliente or Corretor ), example :

//Corretor
Corretor co = db.Pessoa.OfType<Corretor>().where(c => c.Id == 1).FirstOrDefault();

//Cliente
Cliente c = db.Pessoa.OfType<Cliente>().where(c => c.Id == 2).FirstOrDefault();

A clear example would be this link , which can be used as an example as well.

26.04.2017 / 21:45