I have a problem when I try to Scaffolding my ViewModel. Well... First things first. I was having another problem when I try to create my views, it was this problem:
It happens exactly when I try to add a new View using the .Net template for List/Detail/Etc.
Well, after this error show up took me some time to solve it. One of the solutions what I found, it's to change my DbSet to IDBSet. Well... That's worked for a while. But... after I change my DbSet another problem appeared, this time, for each Scaffolding using the template, a new DbSet is created.
Well... I was studying DDD, My code will just below:
public class ProjetoDeEstudoContexto : DbContext
{
public ProjetoDeEstudoContexto()
: base("ProjetoDeEstudoContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ProjetoDeEstudoContexto, Configuration>());
}
public DbSet<Estagiario> Estagiarios { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Properties<string>().Configure(p => p.HasColumnType("varchar"));
modelBuilder.Properties<string>().Configure(p => p.HasMaxLength(150));
//Configuration cria a tabela de acordo com os parametros da classe Config.
modelBuilder.Configurations.Add(new EstagiarioConfig());
}
}
public class EstagiarioConfig : EntityTypeConfiguration<Estagiario>
{
public EstagiarioConfig()
{
HasKey(e => e.EstagiarioId);
Property(n => n.Nome).IsRequired().HasMaxLength(150);
Property(n => n.Sobrenome).IsRequired().HasMaxLength(150);
}
}
public class Estagiario
{
public int EstagiarioId { get; set; }
public string Nome { get; set; }
public string Sobrenome { get; set; }
public DateTime? DataDeInicio { get; set; }
public DateTime? DataDeTermino { get; set; }
}
public class EstagiarioViewModel
{
[Key]
public int EstagiarioId { get; set; }
[Required(ErrorMessage = "Preencha o campo Nome")]
[MaxLength(150, ErrorMessage = "Máximo {0} caracteres")]
[MinLength(2, ErrorMessage = "Minimo {0} caracteres")]
public string Nome { get; set; }
[Required(ErrorMessage = "Preencha o campo Nome")]
[MaxLength(150, ErrorMessage = "Máximo {0} caracteres")]
[MinLength(2, ErrorMessage = "Minimo {0} caracteres")]
public string Sobrenome { get; set; }
[ScaffoldColumn(false)]
public DateTime? DataDeInicio { get; set; }
[ScaffoldColumn(false)]
public DateTime? DataDeTermino { get; set; }
}
My AutoMapper Classes
public static void RegisterMappings()
{
Mapper.Initialize(x =>
{
x.AddProfile<DomainToViewModelMappingProfile>();
x.AddProfile<ViewModelToDomainMappingProfile>();
});
}
public class DomainToViewModelMappingProfile : Profile
{
private MapperConfiguration mapConfig;
public override string ProfileName => "DomainToViewModelMapping";
protected override void Configure()
{
mapConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Estagiario, EstagiarioViewModel>();
});
}
}
public class ViewModelToDomainMappingProfile : Profile
{
private MapperConfiguration mapConfig;
public override string ProfileName => "ViewModelToDomainMapping";
protected override void Configure()
{
mapConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EstagiarioViewModel,Estagiario>();
});
}
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutoMapperConfig.RegisterMappings();
}
}
How I can stop this auto DbSet creation ? What is wrong in my code ?