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

前端 未结 11 1499
慢半拍i
慢半拍i 2020-12-23 19:53

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

相关标签:
11条回答
  • 2020-12-23 20:14

    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.

    0 讨论(0)
  • 2020-12-23 20:15

    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.

    0 讨论(0)
  • 2020-12-23 20:15

    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.

    0 讨论(0)
  • 2020-12-23 20:16

    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.

    0 讨论(0)
  • 2020-12-23 20:20

    I was getting a different error when trying to scaffold a controller with CRUD actions and views. In my case it was saying:

    "There was an error running the selected code generator. Object instance not set to an instance of the object."

    The problem was hard to find: I created a table in SQL Server but forgot to set the Primary Key for the table. Setting the Primary key and updating Entity Framework's .edmx file solved the problem.

    Hope it helps.

    0 讨论(0)
  • 2020-12-23 20:24

    Here is how I resolved this issue. I have Visual Studio 2013 Update 4.
    I use EF Power Tools. Comment out all of the DbSet < ... > Inside OnModelCreating, only leave the object you are scaffolding: modelBuilder.Configurations.Add(new SomeTableDataMap());

    At the bottom of my context class I did notice this got created: public System.Data.Entity.DbSet SomeTableDatas{ get; set; }

    Oh: I also put this in my constructor but it's for something else, this.Configuration.LazyLoadingEnabled = false;

    Seriously, this worked today, I've tried all of these solutions nothing worked for Update 4. I had this working in Update 2 and Update 3 using previous solutions. This is the most up-to-date solution for now.

    0 讨论(0)
提交回复
热议问题