Im unable to scaffold a controller (MVC5 Controller with views, using Entity Framework) in Visual studio 2013 (update 3 and 4). The error message is below:
There was
I also had the same issue and changing my context class to use IDbSet allowed me to successfully use the scaffolding to create a new controller. But since I didn't want to give up the async methods I changed the context back to use DbSet and that worked for me.
I solved it by adding a try/catch on the code to OnModelCreating function inside the context class. Just keep the base.OnModelCreating(modelBuilder);
outside your try/catch
None of the rest of the answers worked for me. What I found out was that the issue only happened when scaffolding and adding Configurations using the Fluent API. So what I did was, instead of having separated files, each one having an Entity Configuration like this:
public class ApplicationUserMapConfiguration : EntityTypeConfiguration<ApplicationUserMap>
{
public ApplicationUserMapConfiguration()
{
ToTable("ApplicationUserMap", "Users");
HasKey(c => c.Id);
}
}
And then adding this configuration to the DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ApplicationUserMapConfiguration());
}
I just added the whole configuration inside the DbContext for every entity:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//ApplicationUser
modelBuilder.Entity<ApplicationUser>().HasKey(c => c.Id);
modelBuilder.Entity<ApplicationUser>().ToTable("ApplicationUser", "Usuario");
//Other entities...
}
Now I can scaffold perfectly. I already submited and issue on the Mvc GitHub.
Also, if another error message raises up saying:
There was an error running the selected code generator: ‘Exception has been thrown by the target of an invocation.’
You should modify your DbContext constructor to:
public YourDbContext() : base("YourDbContext", throwIfV1Schema: false) { }
None of answers of this post worked for me. I handled this issue creating new context class through plus button in Add Controller scaffolding dialog. Once VS created controller and views, I just remove the created context class and change the the generated controller code to use my existing context class.
Important: This process will add a new connection string for the new context, dont forget to remove it as well.
mine got fixed like this:
public virtual DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//--> EntityTypeConfiguration<Your Model Configuration>
modelBuilder.Configurations.Add(new EntityTypeConfiguration<CategoryMapping>());
base.OnModelCreating(modelBuilder);
}
DOn't forget Ctrl + Shift + B so your code compile (i'm not sure for single solution, but since mine is in another project in same solution, it should get compiled first)