Entity Framework 4 - Error Migrations: The type ... is not defined in namespace namespace.Map (Alias = Self)

1

I have a problem with my application mappings.

[EDITED]

Follow the complete DataContext Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using SisprodIT2.Areas.Setor.Models;
using SisprodIT2.Areas.Perfil.Models;
using SisprodIT2.Areas.Telefone.Models;
using SisprodIT2.Areas.Endereco.Models;
using SisprodIT2.Areas.Email.Models;
using SisprodIT2.Models;
using SisprodIT2.Areas.Funcionario.Models;
using SisprodIT2.Areas.Categoria.Models;
using SisprodIT2.Areas.Finalizacao.Models;
using SisprodIT2.Areas.Comentario.Models;
using SisprodIT2.Areas.Chamado.Models;

namespace SisprodIT2.Map
{
    public class DataContext : DbContext
    {
        public DataContext()
            : base("DefaultConnection")
        {
            Configuration.LazyLoadingEnabled = false;
            Configuration.ProxyCreationEnabled = false;
        }

        public DbSet<SetorModel> Setores { get; set; }
        public DbSet<PerfilModel> Perfis { get; set; }
        public DbSet<TelefoneModel> Telefones { get; set; }
        public DbSet<EnderecoModel> Enderecos { get; set; }
        public DbSet<EmailModel> Emails { get; set; }
        public DbSet<FuncionarioModel> Funcionarios { get; set; }
        public DbSet<CategoriaModel> Categorias { get; set; }
        public DbSet<FinalizacaoModel> Finalizacoes { get; set; }
        public DbSet<ComentarioModel> Comentarios { get; set; }
        public DbSet<ChamadoModel> Chamados { get; set; }

        protected override void  OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Configurations.Add(new SetorMap());
            modelBuilder.Configurations.Add(new PerfilMap());
            modelBuilder.Configurations.Add(new TelefoneMap());
            modelBuilder.Configurations.Add(new EnderecoMap());
            modelBuilder.Configurations.Add(new EmailMap());
            modelBuilder.Configurations.Add(new FuncionarioMap());
            modelBuilder.Configurations.Add(new CategoriaMap());
            modelBuilder.Configurations.Add(new FinalizacaoMap());
            modelBuilder.Configurations.Add(new ComentarioMap());
            modelBuilder.Configurations.Add(new ChamadoMap());

            base.OnModelCreating(modelBuilder);
        }
    }
}

Here is the full FunctionalModel class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SisprodIT2.Areas.Setor.Models;
using SisprodIT2.Areas.Email.Models;
using SisprodIT2.Areas.Perfil.Models;
using SisprodIT2.Areas.Endereco.Models;
using SisprodIT2.Areas.Telefone.Models;
using SisprodIT2.Models;
using System.ComponentModel.DataAnnotations;
using SisprodIT2.Areas.Chamado.Models;

namespace SisprodIT2.Areas.Funcionario.Models
{
    public class FuncionarioModel : BaseCadastro
    {
        public int FuncionarioModelId { get; set; }

        [Display(Name="Nome")]
        public string Nome { get; set; }

        [Display(Name="CPF")]
        public string CPF { get; set; }

        [Display(Name = "RG")]
        public string RG { get; set; }

        [Display(Name = "Data de Nascimento")]
        public DateTime Nascimento { get; set; }

        [Display(Name = "Altura")]
        public float Altura { get; set; }

        [Display(Name = "Usuário")]
        public string Usuario { get; set; }

        [Display(Name = "Senha")]
        public string Senha { get; set; }

        [Display(Name = "Perfil")]
        public int PerfilModelId { get; set; }

        [Display(Name = "Setor")]
        public int SetorModelId { get; set; }

        public virtual SetorModel Setor { get; set; }
        public virtual PerfilModel Perfil { get; set; }
        public ICollection<EmailModel> EmailLista { get; set; }
        public ICollection<EnderecoModel> EnderecoLista { get; set; }
        public ICollection<TelefoneModel> TelefoneLista { get; set; }
        public ICollection<ChamadoModel> ChamadoLista { get; set; }

        public FuncionarioModel()
        {
            TelefoneLista = new List<TelefoneModel>();
            EnderecoLista = new List<EnderecoModel>();
            EmailLista = new List<EmailModel>();
            ChamadoLista = new List<ChamadoModel>();
        }
    }
}

Here is the complete CallModel class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SisprodIT2.Areas.Funcionario.Models;
using SisprodIT2.Areas.Categoria.Models;
using SisprodIT2.Models;
using SisprodIT2.Areas.Comentario.Models;
using SisprodIT2.Areas.Finalizacao.Models;
using System.ComponentModel.DataAnnotations;

namespace SisprodIT2.Areas.Chamado.Models
{
    public class ChamadoModel : BaseCadastro
    {
        [Display(Name="ID")]
        public int ChamadoModelId { get; set; }

        [Display(Name = "Título")]
        public string Titulo { get; set; }

        [Display(Name = "Revisão")]
        public int Revisao { get; set; }

        [Display(Name = "Status")]
        public string Status { get; set; }

        [Display(Name = "Descrição do Problema")]
        public string Descricao { get; set; }

        [Display(Name = "Categoria")]
        public int CategoriaModelId { get; set; }

        [Display(Name = "Criador do Chamado")]
        public int FuncionarioCriadorId { get; set; }

        [Display(Name = "Atribuido a")]
        public int FuncionarioResponsavelId { get; set; }

        [Display(Name = "Cod Finalização")]
        public int FinalizacaoModelId { get; set; }

        public virtual FuncionarioModel FuncionarioCriador { get; set; }
        public virtual FuncionarioModel FuncionarioResponsavel { get; set; }
        public virtual CategoriaModel Categoria { get; set; }
        public virtual FinalizacaoModel Finalizacao { get; set; }
        public virtual ICollection<ComentarioModel> ComentarioLista { get; set; }

        public ChamadoModel()
        {
            ComentarioLista = new List<ComentarioModel>();
            FuncionarioCriador = new FuncionarioModel();
            FuncionarioResponsavel = new FuncionarioModel();
            Finalizacao = new FinalizacaoModel();
            Categoria = new CategoriaModel();
        }
    }
}

The Mapping OfficerMap:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SisprodIT2.Areas.Funcionario.Models;
using SisprodIT2.Areas.Chamado.Models;
using SisprodIT2.Models;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;

namespace SisprodIT2.Map
{
    public class FuncionarioMap : EntityTypeConfiguration<FuncionarioModel>
    {
        public FuncionarioMap()
        {
            HasKey(x => x.FuncionarioModelId);

            Property(x => x.FuncionarioModelId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.Nome).IsRequired();
            Property(x => x.CPF).IsOptional();
            Property(x => x.RG).IsOptional();
            Property(x => x.Nascimento).IsOptional();
            Property(x => x.Altura).IsOptional();
            Property(x => x.Usuario).IsRequired();
            Property(x => x.Senha).IsRequired();
            Property(x => x.PerfilModelId).IsRequired();
            Property(x => x.SetorModelId).IsRequired();

            Property(x => x.DataCadastro).IsRequired();
            Property(x => x.DataAtualizacao).IsOptional();
            Property(x => x.FuncionarioAtualizadorId).IsRequired();
            Property(x => x.Ativo).IsRequired();


            HasRequired(x => x.Setor)
                .WithMany(y => y.FuncionarioLista)
                .HasForeignKey(x => x.SetorModelId)
                .WillCascadeOnDelete(false);

            HasRequired(x => x.Perfil)
                .WithMany(y => y.FuncionarioLista)
                .HasForeignKey(x => x.PerfilModelId)
                .WillCascadeOnDelete(false);

            ToTable("Funcionario");
        }
    }
}

The CallMap mapping:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SisprodIT2.Areas.Chamado.Models;
using SisprodIT2.Areas.Funcionario.Models;
using SisprodIT2.Areas.Categoria.Models;
using SisprodIT2.Areas.Finalizacao.Models;
using SisprodIT2.Models;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;

namespace SisprodIT2.Map
{
    public class ChamadoMap : EntityTypeConfiguration<ChamadoModel>
    {
        public ChamadoMap()
        {
            HasKey(x => x.ChamadoModelId);

            Property(x => x.ChamadoModelId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            Property(x => x.Titulo).IsRequired();
            Property(x => x.Revisao).IsRequired();
            Property(x => x.Status).IsRequired();
            Property(x => x.Descricao).IsOptional();
            Property(x => x.CategoriaModelId).IsRequired();
            Property(x => x.FuncionarioCriadorId).IsRequired();
            Property(x => x.FuncionarioResponsavelId).IsOptional();
            Property(x => x.FinalizacaoModelId).IsOptional();

            Property(x => x.DataCadastro).IsRequired();
            Property(x => x.DataAtualizacao).IsOptional();
            Property(x => x.FuncionarioAtualizadorId).IsRequired();
            Property(x => x.Ativo).IsRequired();

            HasRequired(x => x.FuncionarioCriador)
                .WithMany(x => x.ChamadoLista)
                .HasForeignKey(x => x.FuncionarioCriadorId)
                .WillCascadeOnDelete(false);

            HasOptional(x => x.FuncionarioResponsavel)
                .WithMany(x => x.ChamadoLista)
                .HasForeignKey(x => x.FuncionarioResponsavelId)
                .WillCascadeOnDelete(false);

            HasRequired(x => x.Categoria)
                .WithMany(y => y.ChamadoLista)
                .HasForeignKey(x => x.CategoriaModelId)
                .WillCascadeOnDelete(false);

            HasOptional(x => x.Finalizacao)
                .WithMany(y => y.ChamadoLista)
                .HasForeignKey(x => x.FinalizacaoModelId)
                .WillCascadeOnDelete(false);

            ToTable("Chamado");


        }
    }
}

Here is the complete error below:

PM> Add-Migration AlteracoesMapeamentos
System.Data.MetadataException: O esquema especificado não é válido. Erros: 
(120,6) : erro 0040: O tipo ChamadoModel_FuncionarioCriador não é definido no namespace SisprodIT2.Map (Alias=Self).
   em System.Data.Metadata.Edm.EdmItemCollection.LoadItems(IEnumerable'1 xmlReaders, IEnumerable'1 sourceFilePaths, SchemaDataModelOption dataModelOption, DbProviderManifest providerManifest, ItemCollection itemCollection, Boolean throwOnError)
   em System.Data.Metadata.Edm.EdmItemCollection.Init(IEnumerable'1 xmlReaders, IEnumerable'1 filePaths, Boolean throwOnError)
   em System.Data.Metadata.Edm.EdmItemCollection..ctor(IEnumerable'1 xmlReaders)
   em System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ToEdmItemCollection(EdmModel model)
   em System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping)
   em System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace..ctor(DbDatabaseMapping databaseMapping)
   em System.Data.Entity.Infrastructure.DbCompiledModel..ctor(DbModel model)
   em System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   em System.Data.Entity.Internal.RetryLazy'2.GetValue(TInput input)
   em System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   em System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
   em System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   em System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   em System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action'1 writeXml)
   em System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
   em System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   em System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   em System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   em System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
   em System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
O esquema especificado não é válido. Erros: 
(120,6) : erro 0040: O tipo ChamadoModel_FuncionarioCriador não é definido no namespace SisprodIT2.Map (Alias=Self).
PM> 

Where am I going wrong?

I'm using Entity Framework 4 with Fluent API.

    
asked by anonymous 05.12.2016 / 03:09

3 answers

1

I found the answer!

According to this Thread , it is not possible to have two navigational properties in the same simple property.

With this, in the FunctionalModel class, I changed the code:

public ICollection<ChamadoModel> ChamadoLista { get; set; }

To:

public ICollection<ChamadoModel> ChamadoListaCriador { get; set; }
public ICollection<ChamadoModel> ChamadoListaResponsavel { get; set; }

and instantiated these Collections in the constructor:

public FuncionarioModel()
    {
        TelefoneLista = new List<TelefoneModel>();
        EnderecoLista = new List<EnderecoModel>();
        EmailLista = new List<EmailModel>();
        ChamadoListaCriador = new List<ChamadoModel>();
        ChamadoListaResponsavel = new List<ChamadoModel>();
    }

In the Call.Map class, I changed the codes:

HasRequired(x => x.FuncionarioCriador)
            .WithMany(x => x.ChamadoLista)
            .HasForeignKey(x => x.FuncionarioCriadorId)
            .WillCascadeOnDelete(false);

HasOptional(x => x.FuncionarioResponsavel)
            .WithMany(x => x.ChamadoLista)
            .HasForeignKey(x => x.FuncionarioResponsavelId)
            .WillCascadeOnDelete(false);

To:

HasRequired(x => x.FuncionarioCriador)
            .WithMany(x => x.ChamadoListaCriador)
            .HasForeignKey(x => x.FuncionarioCriadorId)
            .WillCascadeOnDelete(false);

HasOptional(x => x.FuncionarioResponsavel)
            .WithMany(x => x.ChamadoListaResponsavel)
            .HasForeignKey(x => x.FuncionarioResponsavelId)
            .WillCascadeOnDelete(false);

And now the Add-Migration command has worked 100%.

Thank you all.

    
06.12.2016 / 00:36
1

In the constructor of the CallModel class Initializes the rest

 public ChamadoModel()
    {
        ComentarioLista = new List<ComentarioModel>();
FuncionarioCriador  = new FuncionarioModel ();
FuncionarioResponsavel  = new FuncionarioModel ();
Categoria  = new CategoriaModel();
Finalizacao = new FinalizacaoModel();

    }
    
05.12.2016 / 18:30
1

The mapping class is in another namespace than the namespace of Models .

When defining a new mapping, make sure that you are using the required dependencies.

using SisprodIT2.Models; 
    
05.12.2016 / 18:41