0

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:

Error

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.

DbSet autocreated

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 ?

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • 1
    You don't need `Database.SetInitializer` in the constructor. Set `public DbSet Estagiarios { get; set; }` as `public virtual DbSet Estagiarios { get; set; }` – Cristian Szpisjak Oct 20 '16 at 19:21
  • Well, i tryied your suggestion but I'm still having the same old problem, when I try to create a new View using the Scaffolding. :( http://imgur.com/a/Wlr8x – Fábio Carvalho Oct 21 '16 at 10:02

1 Answers1

0

On your AutoMapper profiles, first, you should only have a constructor. Second, you only need to call the base CreateMap in the Profile object.

No idea about your EF problems, just wanted to point out the issues in AutoMapper.

Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69
  • Thanks for the tip Jimmy. I'm thinking about this EF problem its because DDD architecture, I don't know if Visual Studio can understand what i'm doing. – Fábio Carvalho Oct 24 '16 at 10:18