9

Specifically I am trying to scaffold WebAPI controllers using the Microsoft scaffolding, WebAPI 2.1, MVC 5.1.1 and Visual Studio 2013 Update 2 RC. I've noticed that when I try to add a mapping file like below in the context then I get error messages only when the scaffold runs. I have tried everything I can think of but I still get the messages when adding a line like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new AnswerMap());

Giving me the following error message in a dialog box after the scaffolding has spent 10 or more seconds doing something:

    Error
    There was an error running the selected code generator: 
    'Exception has been thrown by the target of an invocation'

Checking for causes of this on the web I see many different solutions but none help me. Most solutions to stop this error seem to involve, exiting, starting again, rebuilding or combinations of things. Some users seem to not be even able to solve the problem. If I cannot find out more information about what's wrong then it's really difficult.

Hope someone can point me to a place where I could find a log file or give me some suggestion as to how I could fix this problem.

Please note I already reviewed:

Visual Studio 2013 Scaffolding Error

Nothing here helps. I have reinstalled the scaffolding a few times. The problem goes away if I don't add the mapping file and comes back if I add it again. When I just use my context normally everything is okay.

Here's the code that I am using for the context

using Data.Mapping.Enum;
using Entities.Models.Enum;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using Entities.Models.Core;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebRole1.Models
{ 
    public partial class testCertContext1 : DbContext
    {
        public testCertContext1()
            : base("name=testCertContext1")
        {
        }
        public DbSet<Answer> Answers { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Configurations.Add(new AnswerMap2());
        }
    }

    public class AnswerMap2 : EntityTypeConfiguration<Answer>
    {
        public AnswerMap2()
        {
            // Primary Key
            this.HasKey(t => t.AnswerId);

            // Identity
            this.Property(t => t.AnswerId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // Table & Column Mappings
            this.ToTable("Answer");
            this.Property(t => t.AnswerId).HasColumnName("AnswerId");
            this.Property(t => t.Text).HasColumnName("Text");

        }
    }
}
Community
  • 1
  • 1
  • 1
    Show the code for AnswerMap. Is it doing something to cause an exception? – Dismissile Apr 16 '14 at 12:42
  • I will add the code for AnswerMap to the question. However note that it works great when added to the context and when the database is in normal use. The only thing not working is the scaffolding. I really wish there was some way that I could see the message behind this exception so I could get some ideas on what to look at. –  Apr 16 '14 at 12:59
  • can you please let us know what Web API version & Asp.net MVC version is installed in your VS. – Palak Bhansali Apr 18 '14 at 13:05
  • WebAPI 2.1, MVC 5.1.1, Visual Studio 2013 Update 2 RC –  Apr 18 '14 at 15:18
  • 1
    Might be worth checking the Windows event logs to see if perhaps the underlying scaffolding exception was logged there. – Nathan Apr 18 '14 at 17:56
  • As an advice use 1 version lower of Microsoft technologies to encounter few ambiguous errors such this and don't waste your time. Note that such these issues will be solved by owner(Microsoft) after spending some time. regards. – Amirhossein Mehrvarzi Apr 20 '14 at 09:35
  • @AmirHosseinMehrvarzi - I think that's pretty good advice. –  Apr 21 '14 at 03:43
  • Scaffolding typically makes assumptions on where other classes come from and tries to build a subset of the project so the OnModelCreating can run. Try to move your classes (perhaps AnswerMap) into one project. – Yishai Galatzer Apr 21 '14 at 17:11
  • Thanks but I already tried that. Now the AnswerMap is in the very same place as the model. Still no luck. –  Apr 22 '14 at 07:58
  • @Melina, I have a suspiscion on what the problem is. if you remove the mapping and replace with model annotations, then try to scaffold, does that succeed? – Dave Alperovich Apr 22 '14 at 16:27
  • also, wondering, can you scaffold a normal controller with the answer model? – Dave Alperovich Apr 22 '14 at 16:31
  • Dave A. It works fine with model annotations. –  Apr 24 '14 at 07:28
  • @Melina That's what I thought. There is actually a difference between fluent API and annotations. Scaffolding depends on Model annotations. When relationships are caused with Fluent API, they do not interface with MVC well. For example, Fluent API constraints would not trigger Razor validations. – Dave Alperovich Apr 24 '14 at 18:16
  • these are the kinds of issues we run into using Entities for View Models. I often start by scaffolding with POCO's too, but it's a slipperly slope. If you use Annotations for View Models, this problem will not happen. – Dave Alperovich Apr 24 '14 at 18:17
  • Can you add more Logging in the Application .. Find the Source , InnerException , StackTrace etc for the Exception .. It helped me solve similiar problem – spetzz Apr 25 '14 at 07:49

4 Answers4

6

I had a similar problem.
The problem in my case was that i had the connection string saved in another file, different from the classical web.config file, to avoid to save it in my Git repository.
Something like:

  <connectionStrings configSource="cstring.config"/>

Even though it was configured correctly in the other file, it looks like the scaffold does not solve the transformations. In the end he just does not find the database.
So, my workaround is:
- copy your database connection string in web.config
- scaffold
- return to previous state.

Hope it can help

Leonardo Festa
  • 101
  • 1
  • 6
1

I've had the same problem before, I'm using Repository pattern with Unit of Work and I've changed Code First Reverse Engineering template to add metadata to my entities. When I was thinking to undo a lot of things, I've remembered that I was using .Net Framework 4.5.1!
After change target .Net framework, in project properties, to 4.5 the problem gone away! I've changed it in all the projects.

Bruno Nunes
  • 103
  • 1
  • 7
0

For me, I had to ensure <configSettings>, <appSettings>, & <connectionStrings> tags were NOT using a configSource attribute.

Cord Rehn
  • 1,119
  • 14
  • 22
  • I don't have configSource to begin with, but I am still getting the same error when following the tutorial at: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application. The problem occurs when trying to add new scaffolded items as instructed. I am using VS2013 update 5 and sql server 2012 express. – Stack0verflow Nov 05 '15 at 20:08
0

I ran into a similar problem when following this tutorial.

The solution was to put the connectionString tag after configSections in the web.config. Then it worked!

camelCaseCoder
  • 1,447
  • 19
  • 32