Heritage Mapping Type per Type Asp.Net

Asked

Viewed 69 times

1

In my project, I have a type of heritage that I have mastered by TPT. Analyzing the documents and several examples I can’t identify where I’m going wrong, because the mapping is apparently correct, the definitions of the classes, 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 do any search using the Dbset of the subclasses, the query that Identity assembles is searching from the parent class, that is, if I did 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");

          [...]
     }
}
  • 1

    How is your setup DbContext Put the whole class in question? and its classes also and which code is performing the research?

  • And the queries and the Dbcontext????

1 answer

0


Observing: There are adjustments to be made in the abstract class Pessoa must possess the Id which has the purpose of generating the identifier code of the Person table and which will be used to record in the Client and Broker tables which also has a field of Id but, it is not auto increment, including in Corretor don’t need to have the field CodCorretor 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; }
}

Already in the classes Corretor and Cliente you don’t need to have that field Id, only in the tables as explained in the observation.

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


To have the expected effect one must now create a class inheriting from the Dbcontext with the following code:

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

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

To enter 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 recover the information you must use the method OfType<> offering the type corresponds to recovery (which in 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 in this link, which can also be used as an example.

References:

Browser other questions tagged

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