If an inheritance has remained between these entities it is with OfType
the solution, and the Dbset will only contain the Super Class Gente
. Only reinforcing that in Heritage Oftype is indispensable.
Example: 1 Table Only (Discriminator
)
Classes:
public class Gente
{
public int Id { get; set; }
public String Nome { get; set; }
}
public class GenteFisica: Gente
{
public string Cpf { get; set; }
public int CargoId {get;set;}
[ForeignKey("CargoId")]
public virtual Cargo Cargo { get; set; }
}
public class GenteJuridica: Gente
{
public string Cnpj { get; set; }
public String Ie { get; set; }
}
[Table("Cargo")]
public class Cargo
{
public int Id { get; set; }
public String Nome { get; set; }
}
public class Db : System.Data.Entity.DbContext
{
public Db()
: base("Data Source=.\\SqlExpress;initial catalog=generics2;Integrated Security=False;User ID=sa;Password=senha;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False")
{
}
public System.Data.Entity.DbSet<Gente> Gente {get; set;}
public System.Data.Entity.DbSet<Cargo> Cargo { get; set; }
}
Code:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Db db = new Db();
Cargo cargo = new Cargo();
cargo.Nome = "Administrador";
GenteFisica gentefisica = new GenteFisica();
gentefisica.Nome = "Fulano Fisica 1";
gentefisica.Cpf = "12345645600";
gentefisica.Cargo = cargo;
gentefisica.CargoId = cargo.Id
GenteJuridica gentejuridica = new GenteJuridica();
gentejuridica.Nome = "Fulano Fisica 1";
gentejuridica.Cnpj = "12345645600-00";
gentejuridica.Ie = "102030405060";
db.Cargo.Add(cargo);
db.Gente.Add(gentefisica);
db.Gente.Add(gentejuridica);
db.SaveChanges();
GenteFisica gentefi = db.Gente.OfType<GenteFisica>().Where(x => x.Id == 1).FirstOrDefault();
GenteJuridica genteju = db.Gente.OfType<GenteJuridica>().Where(x => x.Id == 2).FirstOrDefault();
db.Dispose();
}
}
}
Generated Base:
Upshot:
Example: 3 tables
Add the Table of System.ComponentModel.Dataannotations.Schem that way
[Table("Gente")]
public class Gente
{
public int Id { get; set; }
public String Nome { get; set; }
}
[Table("GenteFisica")]
public class GenteFisica: Gente
{
public string Cpf { get; set; }
public int CargoId {get;set;}
[ForeignKey("CargoId")]
public virtual Cargo Cargo { get; set; }
}
[Table("GenteJuridica")]
public class GenteJuridica: Gente
{
public string Cnpj { get; set; }
public String Ie { get; set; }
}
[Table("Cargo")]
public class Cargo
{
public int Id { get; set; }
public String Nome { get; set; }
}
public class Db : System.Data.Entity.DbContext
{
public Db()
: base("Data Source=.\\SqlExpress;initial catalog=generics2;Integrated Security=False;User ID=sa;Password=senha;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False")
{
}
public System.Data.Entity.DbSet<Gente> Gente {get; set;}
public System.Data.Entity.DbSet<Cargo> Cargo { get; set; }
}
It will create your base with 4 tables:
Note: the encoding is equal to 1 table (Add, Update, Delete and Select) with the DbSet<Gente>
for GenteFisica
and GenteJuridica
SQL com Order By Nome - Concat - UNION ALL
**Concat**
//Sem o Tipo
var resultado = db.Gente.OfType<GenteFisica>().Select(x => new
{
x.Id,
x.Nome
}).Concat(db.Gente.OfType<GenteJuridica>().Select(g => new
{
g.Id,
g.Nome
})).AsQueryable();
var resultOrdemNome = resultado.OrderBy(x => x.Nome).ToList();
**Concat**
//Com o Tipo
var resultado = db.Gente.OfType<GenteFisica>().Select(x => new
{
x.Id,
x.Nome,
Gente = "GenteFisica"
}).Concat(db.Gente.OfType<GenteJuridica>().Select(g => new
{
g.Id,
g.Nome,
Gente = "GenteJuridica"
})).AsQueryable();
var resultOrdemNome = resultado.OrderBy(x => x.Nome).ToList();
**Concat**
//Com tipo e com Cargo quando tem
var resultado = db.Gente.OfType<GenteFisica>().Select(x => new
{
x.Id,
x.Nome,
Gente = "GenteFisica",
Cargo = x.Cargo.Nome
}).Concat(db.Gente.OfType<GenteJuridica>().Select(g => new
{
g.Id,
g.Nome,
Gente = "GenteJuridica",
Cargo = ""
})).AsQueryable();
var resultOrdemNome = resultado.OrderBy(x => x.Nome).ToList();
Can show how the tables defined in BD?
– ramaral
@ramaral, presenting only the table
Pessoas
. I will present a demonstrated image!– user3628
@ramaral, sorry for the delay, and the bank image was added!
– user3628
LazyLoading
is enabled or disabled?– ramaral
@ramaral, I didn’t set anything up about
LazyLoading
.– user3628