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

Asked

Viewed 292 times

1

I have a problem in mapping my application.

[EDITED]

Follows 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);
        }
    }
}

Follows the full Functionmodel 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>();
        }
    }
}

Follows the complete Chamadomodel 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 Funciomap mapping:

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 mapping of Chamadomap:

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");


        }
    }
}

Follow 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 am using Entity Framework 4 with Fluent API.

  • vc added the Funciomodel mapping in context ?

  • The problem lies in the separation of Models. Apparently, the Fluent API considers that the mapping and the Models are in the same directory. There is some specific reason for this separation?

  • If you are talking about the separation of the Working Model in Workcreator and Workplacesavel, I need them to define who created the call and who will be responsible for answering the call...

  • Yes, but what’s the need to separate by namespaces? Also, what would be the impediment to updating the Entity Framework?

  • I had misread your question. So, I am using ASP.NET MVC Areas for organization reasons. The mappings are in the Root because of the organization as well. Until then, the mappings were running smoothly in this structure. The error only started to occur after I added the Called.Map mapping with the Functioncreator and Functionsponsavel. Before it was only working and it worked 100%. About the Entity Framework, I’m using Visual Studio 2010.

  • @Fbatista, thank you for answering. I added yes, as you can see in the edited text, but the problem persists. Any idea?

  • Its dbcontext class, puts it tbm, all.

Show 2 more comments

3 answers

1

In the constructor of the class Chamadomodel Initializes the others

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

    }
  • It may also be that you are conflicting in this type of relationship public virtual Functionmodel Functioncreator { get; set; }public virtual Functionwork Workspace { get; set; }

  • I started the others as you directed, but the problem still persists.

  • Warleson Reis, the error started to occur precisely when I changed the map "Called.Map" with the "Functioncreator" and "Functionsponsavel". Before it was only "working" and it worked 100%.

1

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

When setting a new map, make sure you’re using the required dependencies.

using SisprodIT2.Models; 
  • I added dependencies and error remains.

  • Could you please put the complete files, with dependencies and namespaces?

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 Functionodel class, I changed the code:

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

for:

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

and installed 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 Called.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 worked 100%.

Thank you all.

  • Good! I’ve been with EF code first for a few years, and I haven’t seen this one yet, another for the repertoire!

Browser other questions tagged

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