ASP的问题。当我尝试添加映射文件时使用NET搭建

时间:2023-02-06 04:02:13

Specifically I am trying to scaffold WebAPI controllers using the Microsoft scaffolding, WebAPI 2.1, MVC 5.1.1 and Visual Studio 2013 Update 2 RC. I've noticed that when I try to add a mapping file like below in the context then I get error messages only when the scaffold runs. I have tried everything I can think of but I still get the messages when adding a line like this:

具体来说,我正在尝试使用Microsoft scaffolding、WebAPI 2.1、MVC 5.1.1和Visual Studio 2013 Update 2 RC来构建WebAPI控制器。我注意到,当我尝试在上下文中添加如下所示的映射文件时,只有在脚手架运行时才会得到错误消息。我已经尝试了所有我能想到的,但是当我添加这样的一行时,我仍然得到消息:

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

Giving me the following error message in a dialog box after the scaffolding has spent 10 or more seconds doing something:

在脚手架用了10秒或更长时间做某事后,在对话框中给我以下错误消息:

    Error
    There was an error running the selected code generator: 
    'Exception has been thrown by the target of an invocation'

Checking for causes of this on the web I see many different solutions but none help me. Most solutions to stop this error seem to involve, exiting, starting again, rebuilding or combinations of things. Some users seem to not be even able to solve the problem. If I cannot find out more information about what's wrong then it's really difficult.

在网上查找原因,我看到了许多不同的解决方案,但没有一个对我有帮助。阻止这个错误的大多数解决方案似乎都涉及,退出,重新开始,重新构建或组合事物。有些用户似乎甚至无法解决这个问题。如果我找不到更多关于问题所在的信息,那就真的很难了。

Hope someone can point me to a place where I could find a log file or give me some suggestion as to how I could fix this problem.

希望有人能给我指出一个地方,我可以找到一个日志文件,或者给我一些建议,关于如何解决这个问题。

Please note I already reviewed:

请注意,我已经审阅过:

Visual Studio 2013 Scaffolding Error

Visual Studio 2013脚手架错误

Nothing here helps. I have reinstalled the scaffolding a few times. The problem goes away if I don't add the mapping file and comes back if I add it again. When I just use my context normally everything is okay.

这里没有什么帮助。我已经重新安装了几次脚手架。如果不添加映射文件,问题就会消失,如果再次添加映射文件,问题就会回来。当我使用我的上下文时,一切正常。

Here's the code that I am using for the context

这是我为上下文使用的代码

using Data.Mapping.Enum;
using Entities.Models.Enum;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using Entities.Models.Core;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebRole1.Models
{ 
    public partial class testCertContext1 : DbContext
    {
        public testCertContext1()
            : base("name=testCertContext1")
        {
        }
        public DbSet<Answer> Answers { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Configurations.Add(new AnswerMap2());
        }
    }

    public class AnswerMap2 : EntityTypeConfiguration<Answer>
    {
        public AnswerMap2()
        {
            // Primary Key
            this.HasKey(t => t.AnswerId);

            // Identity
            this.Property(t => t.AnswerId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // Table & Column Mappings
            this.ToTable("Answer");
            this.Property(t => t.AnswerId).HasColumnName("AnswerId");
            this.Property(t => t.Text).HasColumnName("Text");

        }
    }
}

4 个解决方案

#1


6  

I had a similar problem.
The problem in my case was that i had the connection string saved in another file, different from the classical web.config file, to avoid to save it in my Git repository.
Something like:

我也有类似的问题。我的问题是,我将连接字符串保存在另一个文件中,这与传统的web不同。配置文件,以避免保存在我的Git存储库中。喜欢的东西:

  <connectionStrings configSource="cstring.config"/>

Even though it was configured correctly in the other file, it looks like the scaffold does not solve the transformations. In the end he just does not find the database.
So, my workaround is:
- copy your database connection string in web.config
- scaffold
- return to previous state.

即使在另一个文件中正确地配置了它,看起来脚手架并没有解决转换。最后,他就是找不到数据库。因此,我的工作是:-在web中复制数据库连接字符串。配置-脚手架-返回到以前的状态。

Hope it can help

希望它可以帮助

#2


1  

I've had the same problem before, I'm using Repository pattern with Unit of Work and I've changed Code First Reverse Engineering template to add metadata to my entities. When I was thinking to undo a lot of things, I've remembered that I was using .Net Framework 4.5.1!
After change target .Net framework, in project properties, to 4.5 the problem gone away! I've changed it in all the projects.

我以前也遇到过同样的问题,我使用的是带有工作单元的存储库模式,我还更改了代码First反向工程模板,以便向我的实体添加元数据。当我想撤销很多东西的时候,我记得我在使用。net Framework 4.5.1!在项目属性中,将目标。net框架更改为4.5后,问题就消失了!我在所有的项目中都做了改动。

#3


0  

For me, I had to ensure <configSettings>, <appSettings>, & <connectionStrings> tags were NOT using a configSource attribute.

对于我来说,我必须确保 , & 标记没有使用configSource属性。

#4


0  

I ran into a similar problem when following this tutorial.

在遵循本教程时,我遇到了类似的问题。

The solution was to put the connectionString tag after configSections in the web.config. Then it worked!

解决方案是将connectionString标记放在web.config中的configSections之后。那么它工作!

#1


6  

I had a similar problem.
The problem in my case was that i had the connection string saved in another file, different from the classical web.config file, to avoid to save it in my Git repository.
Something like:

我也有类似的问题。我的问题是,我将连接字符串保存在另一个文件中,这与传统的web不同。配置文件,以避免保存在我的Git存储库中。喜欢的东西:

  <connectionStrings configSource="cstring.config"/>

Even though it was configured correctly in the other file, it looks like the scaffold does not solve the transformations. In the end he just does not find the database.
So, my workaround is:
- copy your database connection string in web.config
- scaffold
- return to previous state.

即使在另一个文件中正确地配置了它,看起来脚手架并没有解决转换。最后,他就是找不到数据库。因此,我的工作是:-在web中复制数据库连接字符串。配置-脚手架-返回到以前的状态。

Hope it can help

希望它可以帮助

#2


1  

I've had the same problem before, I'm using Repository pattern with Unit of Work and I've changed Code First Reverse Engineering template to add metadata to my entities. When I was thinking to undo a lot of things, I've remembered that I was using .Net Framework 4.5.1!
After change target .Net framework, in project properties, to 4.5 the problem gone away! I've changed it in all the projects.

我以前也遇到过同样的问题,我使用的是带有工作单元的存储库模式,我还更改了代码First反向工程模板,以便向我的实体添加元数据。当我想撤销很多东西的时候,我记得我在使用。net Framework 4.5.1!在项目属性中,将目标。net框架更改为4.5后,问题就消失了!我在所有的项目中都做了改动。

#3


0  

For me, I had to ensure <configSettings>, <appSettings>, & <connectionStrings> tags were NOT using a configSource attribute.

对于我来说,我必须确保 , & 标记没有使用configSource属性。

#4


0  

I ran into a similar problem when following this tutorial.

在遵循本教程时,我遇到了类似的问题。

The solution was to put the connectionString tag after configSections in the web.config. Then it worked!

解决方案是将connectionString标记放在web.config中的configSections之后。那么它工作!