Error when trying to insert data from a mixed table

Asked

Viewed 49 times

0

I’m starting an MVC project and I’m having a problem that I can’t fix.

[Table("InfoGeralEmpresa")]
public class InfoGeralEmpresa
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public InfoGeralEmpresa()
    {
        this.Cliente = new HashSet<Cliente>();
        this.Empresa = new HashSet<Empresa>();
        this.Fornecedor = new HashSet<Fornecedor>();
    }

    public int idInfoGeralEmpresa { get; set; }
    public Nullable<int> cnpj { get; set; }
    public string razaoSocial { get; set; }
    public string nomeFantasia { get; set; }
    public string inscEstadual { get; set; }
    public string email { get; set; }
    public int idEndereco { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Cliente> Cliente { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Empresa> Empresa { get; set; }
    public virtual Endereco Endereco { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Fornecedor> Fornecedor { get; set; }
}

And a class that inherits InfoGeralEmpresa

[Table("Empresa")]
public class Empresa : InfoGeralEmpresa
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Empresa()
    {
        this.Cargo = new HashSet<Cargo>();
        this.Cliente = new HashSet<Cliente>();
        this.Fornecedor = new HashSet<Fornecedor>();
        this.Funcionario = new HashSet<Funcionario>();
        this.OrdemServico = new HashSet<OrdemServico>();
        this.Setor = new HashSet<Setor>();
        this.StatusOS = new HashSet<StatusOS>();
    }

    public int idEmpresa { get; set; }
    //public int idInfoGeralEmpresa { get; set; }
    //[ForeignKey("idInfoGeralEmpresa")]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Cargo> Cargo { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Cliente> Cliente { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Fornecedor> Fornecedor { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Funcionario> Funcionario { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OrdemServico> OrdemServico { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Setor> Setor { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<StatusOS> StatusOS { get; set; }
    public virtual InfoGeralEmpresa InfoGeralEmpresa { get; set; }
}

But when will I make the insertion this way:

 db.InfoGeralEmpresa.Add(empresa);
 db.SaveChanges();

The following error appearsinserir a descrição da imagem aqui

I don’t know what else to do, somebody help me.

  • Do you have any reason to use ICollection<Tipo> instead of List<Tipo>? With the code shown I see no reason for it. Also, I believe you do not need to do db.Endereco.Add(empresa.Endereco) because when adding empresa in context, addresses will be created too, maybe you should test and see if this is the problem...

  • What kind of empresa?

No answers

Browser other questions tagged

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