简体   繁体   English

脚手架控制器不适用于Visual Studio 2013更新3和4

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

Im unable to scaffold a controller (MVC5 Controller with views, using Entity Framework) in Visual studio 2013 (update 3 and 4). 我无法在Visual Studio 2013(更新3和4)中搭建控制器(带有视图的MVC5控制器,使用实体框架)。 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. 我通过从“Entity Framework Power Tools Beta 4”工具菜单中选择“逆向工程师代码优先”来创建模型。

Any ideas about what might cause this error? 关于什么可能导致此错误的任何想法?

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. 我为我的一个Model类添加了一些自定义配置,以使用Fluent API添加关系。 This was specified in my dbContext class in the OnModelCreating override using the following: 这是在我的dbContext类中使用以下内容在OnModelCreating覆盖中指定的:

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

Commenting out the above line allowed the Controller scaffolding to run as expected. 注释掉上面的行允许Controller脚手架按预期运行。

VS 2013 update 2 had a problem with this and the scaffolding came up with an unhelpful error with no further information. VS 2013更新2遇到了问题,脚手架出现了一个无用的错误,没有进一步的信息。 In installed Update 3 and it gave just enough detail to track down the underlying issue. 在已安装的Update 3中,它提供了足够的详细信息来跟踪底层问题。

Jeff. 杰夫。

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... 但随着我的DbContext越来越多,需要映射键的模型越来越多,我无法删除Configurations.Add()方法,因为我会收到EF无法找到主键的错误等等......

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. 我发现通过更改我的DBContext派生类来使用IDbSet属性而不是DbSet我可以生成控制器和视图就好了。 However, for me this introduced another issue, IDbSet does not support the async methods. 但是,对我来说这引入了另一个问题,IDbSet不支持异步方法。

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. 如果您的上下文属性是DbSet类型,请尝试将它们更改为IDbSet。 If you aren't generating async controller methods this may work for you. 如果您没有生成异步控制器方法,这可能对您有用。

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. 我也有同样的问题,改变我的上下文类使用IDbSet允许我成功使用脚手架来创建一个新的控制器。 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. 但由于我不想放弃异步方法,我将上下文改回使用DbSet,这对我有用。

Here is how I resolved this issue. 以下是我解决此问题的方法。 I have Visual Studio 2013 Update 4. 我有Visual Studio 2013 Update 4。
I use EF Power Tools. 我使用的是EF Power Tools。 Comment out all of the DbSet < ... > Inside OnModelCreating, only leave the object you are scaffolding: modelBuilder.Configurations.Add(new SomeTableDataMap()); 注释掉所有DbSet <...> Inside OnModelCreating,只留下你脚手架的对象: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; 在我的上下文类的底部,我注意到它已经创建: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; 哦:我也把它放在我的构造函数中,但它是用于别的东西,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. 说真的,今天工作了,我已经尝试了所有这些解决方案,对于Update 4没有任何作用。我使用以前的解决方案在Update 2和Update 3中工作。 This is the most up-to-date solution for now. 这是目前最新的解决方案。

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 eg TempContext. 在脚手架对话框中,我刚添加了一个新的DataContext,例如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. 所有脚手架都按预期工作,然后我可以简单地将TempContext中生成的代码移动到我原来的DbContext中,并将生成的控制器中的TempContext重命名为原始的DbContext。

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. 假设您在应用程序中有一个DbContext(具体来说就是DbContext的子类),并且您正在尝试使用模型类(比如Model)和DbContext以及脚手架控制器/视图。

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. 我猜DbContext上没有“DbSet <Model> Models {get; set;}”属性,但DbSet仍然使用OnModelCreating方法中的代码添加到DbContext中。

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). 在上面的例子中,scaffolding首先尝试检测DbContext上的DbSet属性(仅通过反射 - 这样就不会检测OnModelCreating是否有代码来添加DbSet)并且不给它,但是脚手架将DbSet属性添加到DbContext然后尝试使用该DbContext的脚手架,但是在运行脚手架时,我们创建一个DbContext实例,我们也调用OnModelCreating,此时脚手架失败,因为DbContext中有多个DbSet类型具有相同的模型(一个脚手架添加一个,一个在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] [这不仅适用于正在使用的模型,也适用于该模型中的相关模型,脚手架为所有相关模型添加了DbSet属性]

[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] [另外,脚手架完成后没有看到添加的DbSet,因为如果操作没有成功完成,脚手架会回滚任何更改,就像Jeff提到的那样,错误消息最初很差并且经过改进后给用户提供了一些提示但它仍然不是很清楚发生了什么]

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. 这是脚手架中的一个错误,一个简单的解决方法是在DbContext上使用DbSet属性来处理模型类的所有相关模型,而不是在OnModelCreating中配置它们。

I was getting a different error when trying to scaffold a controller with CRUD actions and views. 尝试使用CRUD操作和视图来构建控制器时,我遇到了不同的错误。 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. 问题很难找到:我在SQL Server创建了一个表,但忘了为表设置Primary Key Setting the Primary key and updating Entity Framework's .edmx file solved the problem. 设置Primary key并更新Entity Framework的.edmx文件解决了这个问题。

Hope it helps. 希望能帮助到你。

None of the rest of the answers worked for me. 其余的答案都不适合我。 What I found out was that the issue only happened when scaffolding and adding Configurations using the Fluent API. 我发现这个问题只发生在脚手架和使用Fluent API添加配置时。 So what I did was, instead of having separated files, each one having an Entity Configuration like this: 所以我所做的是,而不是分离文件,每个文件都有这样的实体配置:

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

And then adding this configuration to the DbContext: 然后将此配置添加到DbContext:

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

I just added the whole configuration inside the DbContext for every entity: 我刚在DbContext中为每个实体添加了整个配置:

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 scaffold perfectly. 现在我可以完美地支架了。 I already submited and issue on the Mvc GitHub . 我已经提交并发布了Mvc GitHub

Also, if another error message raises up saying: 此外,如果另一个错误消息提出说:

There was an error running the selected code generator: 'Exception has been thrown by the target of an invocation.' 运行所选代码生成器时出错:'调用目标已抛出异常。'

You should modify your DbContext constructor to: 您应该将DbContext构造函数修改为:

public YourDbContext() : base("YourDbContext", throwIfV1Schema: false) { }

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. 我在Add Controller scaffolding对话框中通过加号按钮创建了新的上下文类。 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. 一旦VS创建了控制器和视图,我只需删除创建的上下文类并更改生成的控制器代码以使用我现有的上下文类。

Important : This process will add a new connection string for the new context, dont forget to remove it as well. 重要提示 :此过程将为新上下文添加新的连接字符串,不要忘记将其删除。

mine got fixed like this: 我的修复如下:

    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);
    }

DOn't forget Ctrl + Shift + B so your code compile (i'm not sure for single solution, but since mine is in another project in same solution, it should get compiled first) 不要忘记Ctrl + Shift + B所以你的代码编译(我不确定单个解决方案,但因为我的同一解决方案中的另一个项目,它应该先编译)

I solved it by adding a try/catch on the code to OnModelCreating function inside the context class. 我通过在上下文类中的OnModelCreating函数的代码上添加try / catch来解决它。 Just keep the base.OnModelCreating(modelBuilder); 只需保留base.OnModelCreating(modelBuilder);

outside your try/catch 在你的尝试/捕获之外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Visual Studio 2013脚手架与众不同(控制器vs. WebApi)? - Why is the Visual Studio 2013 Scaffolding different (Controller vs. WebApi) ? Visual Studio 2013中的Monogame 3.2无法正常工作吗? - Monogame 3.2 in Visual Studio 2013 doesn't work? visual studio 2022 .NET 6 当 DbContext 在一个单独的项目中时脚手架不起作用 - visual studio 2022 .NET 6 Scaffolding doesn't work when DbContext is in a separate project 我在Visual Studio 2013上的C#代码无法正常工作 - My C# code on Visual Studio 2013 doesn't work properly 在visual studio express 2013中“拖放形成”计时器不起作用 - “drag and drop to form” timer in visual studio express 2013 doesn't work 无法在Visual Studio 2013 U4中搭建控制器 - Can't scaffold controller in Visual Studio 2013 U4 Visual Studio 2013不会在出错时停在代码行上 - Visual Studio 2013 doesn't stop on a code line at error Visual Studio 2013 没有发现单元测试 - Visual Studio 2013 doesn't discover unit tests 无法将Visual Studio 2013环境变量设置为有效 - Can't get setting Visual Studio 2013 Environment Variables to work 为什么脚手架没有按预期工作? - Why doesn't scaffolding work as expected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM