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 ?
– FBatista
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?
– Leonel Sanches da Silva
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...
– Flavio
Yes, but what’s the need to separate by namespaces? Also, what would be the impediment to updating the Entity Framework?
– Leonel Sanches da Silva
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.
– Flavio
@Fbatista, thank you for answering. I added yes, as you can see in the edited text, but the problem persists. Any idea?
– Flavio
Its dbcontext class, puts it tbm, all.
– FBatista