The forest controller does not work with updating versions 3 and 4 on Visual Studio 2013

I was not able to fake a controller (MVC5 Controller with views using Entity Framework) in Visual Studio 2013 (Update 3 and 4). The error message below:

An error occurred while starting the 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 created the models by selecting Engineer Reverse Code First in the Entity Framework Beta 4 Toolkit tool menu.

Any ideas on what might cause this error?

+42
c # visual-studio entity-framework
Jul 26 '14 at 18:30
source share
11 answers

I had the same problem today.

I added some custom configuration for one of my Model classes to add relationships using the free API. This was pointed out in my dbContext class in an dbContext override, using the following:

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

Commenting on the above line, it is acceptable for the Controller scaffolding to run as expected.

There was a problem with this update in VS 2013 version 2, and a useless error appeared on construction platforms without additional information. In the installed update 3 and he gave enough details to track the main problem.

Jeff.

+39
Aug 07 '14 at 9:29
source share
β€” -

I also had this problem. Jeff's solution works in some cases. But as my DbContext grew with a lot of models that needed to map keys, I could not remove the Configurations.Add () methods, because then I got errors that EF could not find primary keys, etc.

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

It seems I can either generate non-asynchronous controllers with settings in place, or using asynchronous methods without configuration classes.

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

+18
Sep 05 '14 at 20:31
source share

I also had the same problem and changing my context class to use IDbSet allowed me to successfully use scaffolding to create a new controller. But since I didn’t want to give up asynchronous methods, I changed the context to use DbSet, and it worked for me.

+6
Oct 22 '14 at 13:14
source share

This is how I solved this problem. I have an update to Visual Studio 2013 4.
I am using EF Power Tools. Comment on all DbSet <...> Inside OnModelCreating, leave the object on which you are building forests: modelBuilder.Configurations.Add (new SomeTableDataMap ());

At the bottom of my context class, I noticed that this was created: public System.Data.Entity.DbSet SomeTableDatas {get; set; }

A: I also put this in my constructor, but this is for something else, this.Configuration.LazyLoadingEnabled = false;

Seriously, today it worked, I tried all these solutions, nothing worked for Update 4. It worked for me in Update 2 and Update 3 using the previous solutions. This is the most advanced solution.

+3
Mar 10 '15 at 16:57
source share

A simple workaround that worked for me (after many other solutions were offered here and in other places in vain ...).

In the forests dialog box, I just added a new DataContext, for example. TempContext. All the scaffolding worked as expected, and then I could just move the generated code in TempContext to the original DbContext and rename the TempContext in the generated controllers to the original DbContext.

+2
Oct 14 '15 at 12:07
source share

There are already many workarounds in this problem. And in this comment I will try to give a basic question why this might be unsuccessful. [Hoping to inform people of the root cause]

Say you have a DbContext (a specific, child class of DbContext) in your application, and you are trying to use a model class (albeit a model), as well as controllers and views of DbContext and forests.

I assume that DbContext did not have the DbSet & Model Model (get; set;} "property, but the DbSet was added to the DbContext using the code in the OnModelCreating method.

In the above case, the scaffolds first try to discover the DbSet property in the DbContext (only by reflection - so as not to detect if OnModelCreating has code to add the DbSet), and if it doesn’t, the scaffolding adds the DbSet property to the DbContext and then tries run a relay using this DbContext, however when we run scaffolding we create an instance of DbContext and we also call OnModelCreating, and at that moment scaffolding fails because there are several types of DbSet in DbContext with the same model (one forest added and one configured th code in OnModelCreating).

[This happens not only for the model used, but also for related models in this model, scaffolding adds DbSet properties for all related models]

[The added DbSet also does not appear after scaffolding has been done because scaffolding rolls back any changes if the operation did not complete successfully, as Jeff said, the error message was bad initially and was improved to give some hint to the user. but he is still not very clear what is going on]

This is a mistake in scaffolding, a simple job will be to use the DbSet property in DbContext for all related models of your model class instead of setting them in OnModelCreating.

+1
Jun 02 '15 at 21:55
source share

I had another error while trying to raise the controller using CRUD actions and views. In my case, he said:

"An error occurred while starting the code generator. Not installed on the object instance."

The problem was hard to find: I created a table in SQL Server , but forgot to install the Primary Key for the table. Installing the Primary Key and updating the Entity Framework .edmx solved the problem.

Hope this helps.

0
Nov 14 '15 at 0:06
source share

None of the other answers worked for me. What I found out was that the problem only occurred when creating forests and adding configurations using the Fluent API. So what I did was not split the files, each of which had an Entity configuration similar to this:

 public class ApplicationUserMapConfiguration : EntityTypeConfiguration<ApplicationUserMap> { public ApplicationUserMapConfiguration() { ToTable("ApplicationUserMap", "Users"); HasKey(c => c.Id); } } 

Then adding this configuration to the DbContext:

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

I just added the whole configuration inside the DbContext for each object:

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //ApplicationUser modelBuilder.Entity<ApplicationUser>().HasKey(c => c.Id); modelBuilder.Entity<ApplicationUser>().ToTable("ApplicationUser", "Usuario"); //Other entities... } 

Now I can get up perfectly. I have already posted and released on Mvc GitHub .

Also, if there is another error message:

An error occurred while starting the selected code generator: "An exception was thrown by the target of the call.

You should change your DbContext constructor to:

 public YourDbContext() : base("YourDbContext", throwIfV1Schema: false) { } 
0
Mar 30 '16 at 12:51
source share

None of the responses from this post worked for me. I handled this problem by creating a new context class button through a plus in the Add Controller scaffolding dialog box. After VS has created the controller and views, I simply delete the created context class and modify the generated controller code to use my existing context class.

It is important . This process will add a new connection string for the new context; be sure to also delete it.

0
Aug 15 '16 at 18:54
source share

mine was fixed as follows:

  public virtual DbSet<Category> Categories { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //--> EntityTypeConfiguration<Your Model Configuration> modelBuilder.Configurations.Add(new EntityTypeConfiguration<CategoryMapping>()); base.OnModelCreating(modelBuilder); } 

Do not forget Ctrl + Shift + B to compile your code (I'm not sure for one solution, but since mine is in another project in the same solution, it must compile first)

0
Jan 19 '17 at 11:42 on
source share

I solved this by adding try / catch in the code to the OnModelCreating function inside the context class. Just keep base.OnModelCreating (MODELBUILDER);

out of your try / catch

0
Aug 02 '17 at 13:11
source share



All Articles