2
I have a doubt in the inheritance part, I have the person and user classes. Person has its basic attributes and the user inherits from the person. So far so good is right.
My problem is that in context mapping I am passing the user, but I would not like you to create all fields of the person in the database, so how can I limit which fields I want to be created in the database ? or there is no way.
Classe Pessoa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace Model
{
public class Pessoa
{
private int codigo;
private int status; // Ativo = 1, Inativo = 0
private string nome;
private string email;
private string cidade;
private string endereco;
private string bairro;
private string numero;
private DateTime dtCriacao;
//Contrutor para heranca para obrigacao da criacao de usuario
public Pessoa( string nome, string email, int status) {
this.nome = nome;
this.email = email;
this.status = status;
this.dtCriacao = DateTime.Now;
}
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Data Criaçao deve estar preenchida")]
public DateTime DtCriacao
{
get { return dtCriacao; }
set { dtCriacao = value; }
}
[Key]
public int Codigo
{
get { return codigo; }
set { codigo = value; }
}
[Required(AllowEmptyStrings = false, ErrorMessage = "Status deve estar entre 1 = Ativo ou 0 = Inativo")]
[Range(0, 1)]
public int Status
{
get { return status; }
set { status = value; }
}
[StringLength(15)]
public string Numero
{
get { return numero; }
set { numero = value; }
}
[StringLength(80)]
public string Bairro
{
get { return bairro; }
set { bairro = value; }
}
[StringLength(100)]
public string Endereco
{
get { return endereco; }
set { endereco = value; }
}
[StringLength(100)]
public string Cidade
{
get { return cidade; }
set { cidade = value; }
}
[Required(AllowEmptyStrings = false, ErrorMessage = "E-mail deve estar preenchido")]
[StringLength(250)]
[EmailAddress(ErrorMessage = "E-mail em formato inválido.")]
public string Email
{
get { return email; }
set { email = value; }
}
[Required(AllowEmptyStrings = false, ErrorMessage = "Nome deve ser preenchido")]
[StringLength(200)]
public string Nome
{
get { return nome; }
set { nome = value; }
}
}
}
User class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Model
{
[Table("Usuario", Schema = "public")]
public class Usuario : Pessoa
{
private string login;
private string senha;
public Usuario(string nome, string email, int status, string login, string senha) : base( nome, email, status)
{
this.login = login;
this.senha = senha;
}
[Required(AllowEmptyStrings = false, ErrorMessage = "Login deve ser preenchido!")]
[StringLength(50)]
[Index("Ix_UsuarioLogin", IsUnique = true)]
public string Login
{
get { return login; }
set { login = value; }
}
[Required(AllowEmptyStrings = false, ErrorMessage = "Senha deve ser preechida!")]
[StringLength(20)]
public string Senha
{
get { return senha; }
set { senha = value; }
}
}
}
Context
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using System.Data.Entity;
using Model;
namespace DAL
{
public class BaseContexto : DbContext
{
public BaseContexto()
: base("Teste")
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("public"); base.OnModelCreating(modelBuilder);
}
public DbSet<Usuario> Usuario { get; set; }
}
}
My problem is here, in the table was created the address data I do not need, type city, address, number... How can I get this data not to be mapped?
My user table:
CREATE TABLE public."Usuario"
(
"Codigo" integer NOT NULL DEFAULT nextval('"Usuario_Codigo_seq"'::regclass),
"Login" character varying(50) NOT NULL DEFAULT ''::character varying,
"Senha" character varying(20) NOT NULL DEFAULT ''::character varying,
"DtCriacao" timestamp without time zone NOT NULL DEFAULT '-infinity'::timestamp without time zone,
"Status" integer NOT NULL DEFAULT 0,
"Numero" character varying(15),
"Bairro" character varying(80),
"Endereco" character varying(100),
"Cidade" character varying(100),
"Email" character varying(250) NOT NULL DEFAULT ''::character varying,
"Nome" character varying(200) NOT NULL DEFAULT ''::character varying,
CONSTRAINT "PK_public.Usuario" PRIMARY KEY ("Codigo")
)
WITH (
OIDS=FALSE
);
ALTER TABLE public."Usuario"
OWNER TO postgres;
-- Index: public."Usuario_Ix_UsuarioLogin"
-- DROP INDEX public."Usuario_Ix_UsuarioLogin";
CREATE UNIQUE INDEX "Usuario_Ix_UsuarioLogin"
ON public."Usuario"
USING btree
("Login" COLLATE pg_catalog."default");
Okay I understand, thanks for the answer I will use the abstract class. Only one doubt that has arisen, the inheritance should only be used when the child class will use all attributes of the parent class?
– Aprendiz
@Apprentice if the parent class has unwanted properties in the daughter class, then this distorts inheritance and possibly points to a problem in modeling.
– Tobias Mesquita
Read about the Interface Segregation Principle, which corresponds to the letter "I" of SOLID: https://robsoncastilho.com.br/2013/04/14/principios-solid-principio-segregaca-de-interface-isp/
– Marcell Alves
Thank you, just one other thing, I can inherit an Abstract class by creating another Abstract? class or it’s wrong. Example:
public abstract class PessoaBase{}
 public abstract class Pessoa : PessoaBase {}

&#For the class person also should not be instantiated in my system, only inherited.– Aprendiz
@Apprentice, an abstract class is still a class, has no problem in it inherits from another class, whether abstract or not.
– Tobias Mesquita