我尝试添加映射文件时ASP.NET脚手架的问题

9 asp.net asp.net-mvc entity-framework asp.net-mvc-scaffolding asp.net-web-api

具体来说,我试图使用Microsoft脚手架,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());
Run Code Online (Sandbox Code Playgroud)

在脚手架花费10秒或更长时间执行某些操作后,在对话框中给出以下错误消息:

    Error
    There was an error running the selected code generator: 
    'Exception has been thrown by the target of an invocation'
Run Code Online (Sandbox Code Playgroud)

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

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

请注意我已经审核过:

Visual Studio 2013脚手架错误

这里没有任何帮助.我已经重新安装了几次脚手架.如果我不添加映射文件,问题就会消失,如果我再次添加它,则会返回.当我只是使用我的上下文时,一切都很好.

这是我用于上下文的代码

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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

我遇到了类似的问题.
我的问题是我将连接字符串保存在另一个文件中,与传统的web.config文件不同,以避免将其保存在我的Git存储库中.
就像是:

  <connectionStrings configSource="cstring.config"/>
Run Code Online (Sandbox Code Playgroud)

即使它在另一个文件中正确配置,看起来脚手架也不能解决转换.最后他只是找不到数据库.
因此,我的解决方法是:
- 在web.config中复制数据库连接字符串
- scaffold
- 返回到先前的状态.

希望它可以提供帮助


Bru*_*nes 1

我以前遇到过同样的问题,我将存储库模式与工作单元一起使用,并且我更改了 Code First 逆向工程模板以将元数据添加到我的实体中。当我考虑撤消很多事情时,我记得我使用的是.Net Framework 4.5.1!
更改目标.Net框架后,在项目属性中,到4.5问题就消失了!我已经在所有项目中更改了它。