I have a question on the inheritance part, I have the classes person and user. Person has its basic attributes and the user inherits from the person. So far so good, that's right.
My problem is that in context mapping I'm passing the user, but I do not want you to create all the person's fields in the database, so how can I limit which fields I want created in the database? or does not exist as.
Person Class
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 that 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");