Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ASP.NET MVC + EF scaffolding be used after implementing EntityTypeConfiguration classes?

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>() or ComplexType<T>() methods.'

Is there a fix (ex. custom code or project configuration)?

like image 601
one.beat.consumer Avatar asked Apr 26 '16 17:04

one.beat.consumer


People also ask

How do you use scaffolding in MVC?

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.

What is meant by scaffolding in MVC?

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.


1 Answers

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.

like image 90
Nadège Rouelle Avatar answered Sep 25 '22 00:09

Nadège Rouelle