Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaffolding controller doesn't work with visual studio 2013 update 3 and 4

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 an error running the selected code generator:

A configuration for type 'Library.Morthwind.Models.Catgeory' has already been added. To reference the existing configuration use the Entity<T>() or ComplexType<T>() methods

I have created the models by selecting 'Reverse Engineer Code First' from the 'Entity Framework Power Tools Beta 4' Tool menu.

Any ideas about what might cause this error?

like image 617
SimonB Avatar asked Jul 26 '14 18:07

SimonB


3 Answers

I had the same issue today.

I had added some custom configuration for one of my Model classes to add a relationship using the fluent API. This was specified in my dbContext class in the OnModelCreating override using the following:

modelBuilder.Configurations.Add(new OrderConfiguration()); 

Commenting out the above line allowed the Controller scaffolding to run as expected.

VS 2013 update 2 had a problem with this and the scaffolding came up with an unhelpful error with no further information. In installed Update 3 and it gave just enough detail to track down the underlying issue.

Jeff.

like image 179
Jeff Avatar answered Sep 21 '22 02:09

Jeff


I've been having this issue too. Jeff's solution works in some cases. But as my DbContext grew with more models that had keys needing to be mapped I couldn't delete the Configurations.Add() methods because I would then get errors that EF couldn't find primary keys, etc...

I did discover that by changing my DBContext derived class to use IDbSet properties instead of DbSet I could generate the controllers and views just fine. However, for me this introduced another issue, IDbSet does not support the async methods.

It appears I can either generate non-async controllers with configurations in place or async methods without configuration classes.

If your context properties are of type DbSet, try changing them to IDbSet. If you aren't generating async controller methods this may work for you.

like image 31
Brian Scott Avatar answered Sep 23 '22 02:09

Brian Scott


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.

like image 31
Gandyman Avatar answered Sep 25 '22 02:09

Gandyman