C # - I can not map HERIT with the Entity Framework Core - Code First?

0

I am not able to map the Person and Client classes ( Table Per Type ( TPT ) using Entity Framework Core ), which Client inherits from Person.

How do I do it?

public class Pessoa
{
    public int PessoaId { get; set; }
}


public class Cliente : Pessoa
{
}

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

public void PessoaMapping(ModelBuilder modelBuilder)
{
       modelBuilder.Entity<Pessoa>()
                .ToTable("tblPessoa");

       modelBuilder.Entity<Pessoa>()
                .HasKey(p => p.PessoaId);

       modelBuilder.Entity<Pessoa>()
                .Property(p => .PessoaId).ValueGeneratedNever();
}

public void ClienteMapping(ModelBuilder modelBuilder)
{
      modelBuilder.Entity<Cliente>()
             .ToTable("tblCliente");
}
    
asked by anonymous 25.07.2017 / 23:14

1 answer

0

Created classes are only set up in their context in the OnModelCreating method or even by DataAnnotations , this minimum example is set by the OnModelCreating method:

public class Pessoa
{
    public int PessoaId { get; set; }
    public string Nome { get; set; }
}

public class Cliente: Pessoa
{
    public string CPF { get; set; }
}
using Microsoft.EntityFrameworkCore;

public class Ctx: DbContext
{
    public Ctx()
    {
        //Database.EnsureCreated();
    }

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

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {            
        string cn = "Server=.\SQLExpress;Database=MyDataBaseEntityCore;";
        cn += "User Id=sa;Password=senha;";

        optionsBuilder.UseSqlServer(cn, c =>
        {

        });
    }

    //configurando as classes
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Pessoa>()
            .ToTable("Pessoa")
            .ForSqlServerToTable("Pessoa")
            .HasKey(x => x.PessoaId);

        modelBuilder.Entity<Pessoa>()
            .Property(x => x.PessoaId)
            .IsRequired()
            .UseSqlServerIdentityColumn();

        modelBuilder.Entity<Pessoa>()
            .Property(x => x.Nome)
            .HasMaxLength(100)
            .IsRequired();


        modelBuilder.Entity<Cliente>()
            .ForSqlServerToTable("Cliente")
            .ToTable("Cliente");

        modelBuilder.Entity<Cliente>()
            .Property(x => x.CPF)
            .IsRequired()
            .HasMaxLength(20);

    }
}

One point to note is that this version ( 1.1.2 ) of the Entity Framework Core, even configuring the tables, does not generate a table for each type (as requested in the question), but a Discriminator field to identify which type belongs to that record, I believe it's still a limitation of the version.

Remarks: Just remember that this setting is for Entity Framework Core 1.1.2 for the SQLServer bank, which is the most stable but is in the kiln almost ready version 2.0.0 , which will bring many changes.

Operations:

  • Add new Cliente :

    using (Ctx c = new Ctx())
    {
        c.Pessoa.Add(new Cliente
        {
            CPF = "111.111.111-11",
            Nome = "StackOverFlow Core"
        });
        c.SaveChanges();
    }
    
  • Edit Cliente of PessoaId = 1 :

    using (Ctx c = new Ctx())
    {
         Cliente cliente = c.Pessoa
            .OfType<Cliente>()
            .FirstOrDefault(x => x.PessoaId == 1);
    
        if (cliente != null)
        {
            cliente.Nome = "StackOverFlow Core + Update";
            c.SaveChanges();
        }    
    }
    
  • Delete Cliente of PessoaId = 1 :

    using (Ctx c = new Ctx())
    {
        Cliente cliente = c.Pessoa
            .OfType<Cliente>()
            .FirstOrDefault(x => x.PessoaId == 1);
        if (cliente != null)
        {
            c.Pessoa.Remove(cliente);
            c.SaveChanges();
        }                
    }
    
  • Search Cliente of PessoaId = 2 :

    using (Ctx c = new Ctx())
    {
        Cliente cliente = c.Pessoa
            .OfType<Cliente>()
            .FirstOrDefault(x => x.PessoaId == 2);
        if (cliente != null)
        {
    
        }                
    }
    

26.07.2017 / 03:44