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?
Simple workaround that worked for me (after trying many other solutions suggested here and other places in vain...).
In the scaffolding dialog I just added a new DataContext e.g. TempContext. All the scaffolding worked as expected and then I could simply move the generated code in TempContext into my original DbContext and renamed the TempContext in the generated controllers to the original DbContext.
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 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 theOnModelCreating
override using the following: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.
There are already multiple workarounds posted for this issue. And in this comment, I will try to provide the underlying issue why this may be failing. [With the hope to make people aware of the root cause]
Let's say, you have a DbContext (to be specific, child class of DbContext) in your application, and you are trying to use a model class (let's say Model) and the DbContext and scaffolding controllers / views.
I am guessing that the DbContext did not have a "DbSet< Model > Models {get; set;}" property on it but the DbSet was nevertheless added to the DbContext using code in OnModelCreating method.
In the above case, scaffolding first tries to detect DbSet property on DbContext (by reflection only - so that does not detect if OnModelCreating has code to add the DbSet) and given it's not, scaffolding adds a DbSet property to the DbContext and then tries to scaffold using that DbContext , however when running the scaffolding, we create an instance of DbContext and we also call OnModelCreating , and at that point, scaffolding fails because there are multiple DbSet types with the same model in the DbContext (one added by scaffolding and one configured in code in OnModelCreating).
[This happens not only for the model being used but also for related models in that model , scaffolding adds DbSet properties for all related models]
[Also, one doesn't see the added DbSet's after the scaffolding is done because scaffolding rolls back any changes if the operation did not complete successfully, like Jeff mentioned , the error message was poor initially and was improved to give some hint to the user but it's still not super clear what's going on]
This is a bug in scaffolding , a simple work around would be to use DbSet property on DbContext for all related models of your model class instead of configuring them in OnModelCreating.
I was getting a different error when trying to scaffold a controller with CRUD actions and views. In my case it was saying:
The problem was hard to find: I created a table in
SQL Server
but forgot to set thePrimary Key
for the table. Setting thePrimary key
and updating Entity Framework's.edmx
file solved the problem.Hope it helps.
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.