Error in Discriminator Column EF code first

Asked

Viewed 688 times

0

I am doing a project in Asp.net, I am using classes in C# to make the connection with the bank, however, every time I try to insert a user in the bank, generates the following error:

invalid "Discriminator" column name

Note: My bank already existed, I used the code first from database.

This is my method of registering:

public virtual void cadastrar(T item) 
{
    context.Set<T>().Add(item);
    context.SaveChanges();

}

The class generated by the RU:

using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

public partial class Banco : DbContext
{
    public Banco()
        : base("BDbuffet")
    {
    }

    public virtual DbSet<Alimento> Alimentos { get; set; }
    public virtual DbSet<Bebida> Bebidas { get; set; }
    public virtual DbSet<Festa> Festas { get; set; }
    public virtual DbSet<Garcon> Garcons { get; set; }
    public virtual DbSet<GarconsContratado> GarconsContratados { get; set; }
    public virtual DbSet<Iten> Itens { get; set; }
    public virtual DbSet<sysdiagram> sysdiagrams { get; set; }
    public virtual DbSet<Usuario> Usuarios { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Alimento>()
            .Property(e => e.nome)
            .IsUnicode(false);

        modelBuilder.Entity<Alimento>()
            .Property(e => e.descricao)
            .IsUnicode(false);

        modelBuilder.Entity<Alimento>()
            .Property(e => e.imagemAlimento)
            .IsUnicode(false);

        modelBuilder.Entity<Alimento>()
            .Property(e => e.valorUnitario)
            .HasPrecision(19, 4);

        modelBuilder.Entity<Alimento>()
            .HasMany(e => e.Itens)
            .WithOptional(e => e.Alimento)
            .HasForeignKey(e => e.idAlimentos);

        modelBuilder.Entity<Bebida>()
            .Property(e => e.nome)
            .IsUnicode(false);

        modelBuilder.Entity<Bebida>()
            .Property(e => e.descricao)
            .IsUnicode(false);

        modelBuilder.Entity<Bebida>()
            .Property(e => e.imagemBebida)
            .IsUnicode(false);

        modelBuilder.Entity<Bebida>()
            .Property(e => e.valorUnitario)
            .HasPrecision(19, 4);

        modelBuilder.Entity<Bebida>()
            .HasMany(e => e.Itens)
            .WithOptional(e => e.Bebida)
            .HasForeignKey(e => e.idBebidas);

        modelBuilder.Entity<Festa>()
            .Property(e => e.local)
            .IsUnicode(false);

        modelBuilder.Entity<Festa>()
            .Property(e => e.data)
            .IsUnicode(false);

        modelBuilder.Entity<Festa>()
            .Property(e => e.status)
            .IsUnicode(false);

        modelBuilder.Entity<Festa>()
            .Property(e => e.valorTotal)
            .HasPrecision(19, 4);

        modelBuilder.Entity<Festa>()
            .HasMany(e => e.GarconsContratados)
            .WithRequired(e => e.Festa)
            .HasForeignKey(e => e.idFesta)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Festa>()
            .HasMany(e => e.Itens)
            .WithOptional(e => e.Festa)
            .HasForeignKey(e => e.idFesta);

        modelBuilder.Entity<Garcon>()
            .Property(e => e.nome)
            .IsUnicode(false);

        modelBuilder.Entity<Garcon>()
            .Property(e => e.cpf)
            .IsUnicode(false);

        modelBuilder.Entity<Garcon>()
            .Property(e => e.sexo)
            .IsUnicode(false);

        modelBuilder.Entity<Garcon>()
            .Property(e => e.telefone)
            .IsUnicode(false);

        modelBuilder.Entity<Garcon>()
            .Property(e => e.email)
            .IsUnicode(false);

        modelBuilder.Entity<Garcon>()
            .HasMany(e => e.GarconsContratados)
            .WithRequired(e => e.Garcon)
            .HasForeignKey(e => e.idGarcon)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<GarconsContratado>()
            .Property(e => e.fone)
            .IsUnicode(false);

        modelBuilder.Entity<GarconsContratado>()
            .Property(e => e.email)
            .IsUnicode(false);

        modelBuilder.Entity<GarconsContratado>()
            .Property(e => e.valorPago)
            .HasPrecision(19, 4);

        modelBuilder.Entity<GarconsContratado>()
            .Property(e => e.status)
            .IsUnicode(false);

        modelBuilder.Entity<Iten>()
            .Property(e => e.valorTotal)
            .HasPrecision(19, 4);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.nome)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.cpf)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.endereco)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.cidade)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.fone)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.login)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.senha)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.aceitoTermos)
            .IsUnicode(false);

        modelBuilder.Entity<Usuario>()
            .Property(e => e.email)
            .IsUnicode(false);
    }
}
  • Errors in the "Discriminator" field are usually related to some case of inheritance between classes mapped in EF, as it is standard for it to use such a field to define who is the class that should implement the record. In your case, there is some class you inherit from User?

  • Yes has two classes, Client and administrator who inherits User

  • How do I implement the record ?

  • If these classes do not need to be mapped (as it seems from your example), you should make it explicit pro EF that they should not be included in the mapping. For example: modelBuilder.Ignore<Typesview>();

  • Did not modify anything continues with the same error, I tried to register in another class that does not inheritance with another and the same error occurred

  • and this error also appeared "The Entity type Client is not part of the model for the Current context."

  • But do these Client and Administrator classes have fields that are part of your base? They should be mapped?

  • In fact, what would be the relationship between User, Client and Administrator in both the base and its classes?

  • The user table would have everyone using the system, which would include Adm and Client

  • I could see the problem, it was with the relationship with Adm

  • Thanks for the tips

  • You’re welcome! If you can, please create an answer indicating what was missing, to help future users with this same problem.

  • I found the error, The code first when it maps the bank creates a column more called discriminator, what I was generating was that I made a class inherit the administrator inherit Users that corresponds to the table in the bank, but this class Adm had no discriminator column because it was not from the bank, then the error occurred, after removing this inheritance the problem was solved.

  • There is one thing there that you don’t need no matter the public virtual Dbset<sysdiagram> sysdiagrams { get; set; }.... do not select anything that has Diagram in your tables

Show 9 more comments
No answers

Browser other questions tagged

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