Background
Visual Studio scaffolding for new ASP.NET MVC Controllers bound to Entity Framework work well when the models use data annotations or the direct lines within OnModelCreating(DbModelBuilder)
to describe their characteristics.
Issue
However, in our scenario, the content of OnModelCreating
is refactored using individual EntityTypeConfiguration<T>
classes. When attempting to create a new Controller via the MVC+EF scaffolding the following pop up error occurs:
There was an error running the selected code generator: 'A configuration for type SomeModelClass has already been added. To reference the existing configuration use the
Entity<T>()
orComplexType<T>()
methods.'
Is there a fix (ex. custom code or project configuration)?
Add a scaffolded item to MVC or Web API To add a scaffold, right-click either the project or a folder within the project, and select Add – New Scaffolded Item, as shown in the following image. From the Add Scaffold window, select the type of scaffold to add.
Scaffolding is used to define the code-generation framework used in web applications. It uses T4 templates to generate basic controllers and views for the models. It generates instances for the mapped domain model and code for all CRUD operations.
Here is something that seems to be the same issue as yours : Scaffolding controller doesn't work with visual studio 2013 update 3 and 4
This is a soluton that seems to work :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With