Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.MVC:运行所选代码生成器时出错“;documentId不是来自visual studio workspace的;参数id:documentId_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net mvc ASP.MVC:运行所选代码生成器时出错“;documentId不是来自visual studio workspace的;参数id:documentId

Asp.net mvc ASP.MVC:运行所选代码生成器时出错“;documentId不是来自visual studio workspace的;参数id:documentId,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我试图在visual studio 2015中使用EF 6.1.3中的代码优先方法。一切都进行得很顺利,项目已经构建,但是如果我想添加一个“新的scaffold项”->“MVC控制器和视图,使用EF”,它总是给我以下错误: Error running selected code generator. The given documentId did not come from visual studio workspace" Parameter id: documentId 如果我使用

我试图在visual studio 2015中使用EF 6.1.3中的代码优先方法。一切都进行得很顺利,项目已经构建,但是如果我想添加一个“新的scaffold项”->“MVC控制器和视图,使用EF”,它总是给我以下错误:

Error running selected code generator. 
The given documentId did not come from visual studio  workspace"  
Parameter id: documentId
如果我使用其他任何一个控制器,它都可以正常工作,但我不想自己键入所有代码。我有以下文件,任何帮助将不胜感激。干杯

-----------------------------------------------------------------------------------------------------模型--------------------------------------------------- 模型->课程.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace ContosoUniversity.Models
{
    public class Course
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Key]
        public int CourseID { get; set; }
        public string Title { get; set; }
        public int Credits { get; set; }

        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }
}
DAL->SchoolContext.cs

using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace ContosoUniversity.DAL
{
    public class SchoolContext : DbContext
    {
        public SchoolContext() : base("SchoolContext")
        {
        }
        public DbSet<Student> Students { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Course> Courses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}
使用ContosoUniversity.Models;
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Conventions;
名称空间ContosoUniversity.DAL
{
公共课堂背景:DbContext
{
公共学校上下文():基础(“学校上下文”)
{
}
公共数据库集学生{get;set;}
公共数据库集注册{get;set;}
公共数据库集课程{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
}
DAL->初始值设定项(初始化数据文件)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用ContosoUniversity.模型;
名称空间ContosoUniversity.DAL
{
公共类初始值设定项:System.Data.Entity.DropCreateDatabaseIfModelChanges
{
受保护的覆盖无效种子(学校上下文)
{
var students=新列表
{
新学生{FirstMidName=“Carson”,LastName=“Alexander”,registrmentdate=DateTime.Parse(“2005-09-01”)},
新学生{FirstMidName=“Meredith”,LastName=“Alonso”,registrmentdate=DateTime.Parse(“2002-09-01”)},
新学生{FirstMidName=“Arturo”,LastName=“Anand”,registrmentdate=DateTime.Parse(“2003-09-01”)},
新学生{FirstMidName=“Gytis”,LastName=“Barzdukas”,registrmentdate=DateTime.Parse(“2002-09-01”)},
新学生{FirstMidName=“Yan”,LastName=“Li”,registrmentdate=DateTime.Parse(“2002-09-01”),
新学生{FirstMidName=“Peggy”,LastName=“Justice”,registrmentdate=DateTime.Parse(“2001-09-01”),
新学生{FirstMidName=“劳拉”,LastName=“诺曼”,registrmentdate=DateTime.Parse(“2003-09-01”)},
新学生{FirstMidName=“Nino”,LastName=“Olivetto”,registrmentdate=DateTime.Parse(“2005-09-01”)}
};
ForEach(s=>context.students.Add);
SaveChanges();
var课程=新列表
{
新课程{CourseID=1050,Title=“化学”,学分=3,},
新课程{CourseID=4022,Title=“微观经济学”,学分=3,},
新课程{CourseID=4041,Title=“宏观经济学”,学分=3,},
新课程{CourseID=1045,Title=“微积分”,学分=4,},
新课程{CourseID=3141,Title=“三角学”,学分=4,},
新课程{CourseID=2021,Title=“作文”,学分=3},
新课程{CourseID=2042,Title=“文学”,学分=4}
};
courses.ForEach(c=>context.courses.Add(c));
SaveChanges();
var注册=新列表
{
新入学人数{StudentID=1,CourseID=1050,Grade=Grade.A},
新入学人数{StudentID=1,CourseID=4022,Grade=Grade.C},
新入学人数{StudentID=1,CourseID=4041,Grade=Grade.B},
新入学人数{StudentID=2,CourseID=1045,Grade=Grade.B},
新入学人数{StudentID=2,CourseID=3141,Grade=Grade.F},
新入学人数{StudentID=2,CourseID=2021,Grade=Grade.F},
新入学人数{StudentID=3,CourseID=1050},
新入学人数{StudentID=4,CourseID=1050},
新入学人数{StudentID=4,CourseID=4022,Grade=Grade.F},
新入学人数{StudentID=5,CourseID=4041,Grade=Grade.C},
新入学人数{StudentID=6,CourseID=1045},
新入学人数{StudentID=7,CourseID=3441,Grade=Grade.A},
};
Enrolments.ForEach(e=>context.Enrolments.Add(e));
SaveChanges();
}
}
}
根目录中的Web配置

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>

  <connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=ContosoUniversity1; Integrated Security=SSPI;"  providerName="System.Data.SqlClient" />
  </connectionStrings>

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
  </system.webServer>


  <entityFramework>
    <contexts>
      <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
        <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity"></databaseInitializer>
      </context>

    </contexts>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>


</configuration>


我今天遇到了这个问题,我只是卸载/重新加载(右键单击VisualStudio中的项目“卸载项目”,然后再次右键单击“重新加载项目”)VisualStudio中的项目,然后重建项目,我就能够创建控制器。

我今天遇到了这个问题,我只是卸载/重新加载(右键单击VisualStudio中的项目“卸载项目”,然后再次右键单击“重新加载项目”)在VisualStudio中创建项目,然后重建项目,我就可以创建控制器了。

Hi@Rodney,你能修复这个问题吗?我得到了同样的错误,我不知道它的根本原因是什么。Hi@Rodney,你能修复这个问题吗?我得到了同样的错误,我不知道它的根本原因是什么。
using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace ContosoUniversity.DAL
{
    public class SchoolContext : DbContext
    {
        public SchoolContext() : base("SchoolContext")
        {
        }
        public DbSet<Student> Students { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Course> Courses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ContosoUniversity.Models;

namespace ContosoUniversity.DAL
{
    public class SchoolInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<SchoolContext>
    {

        protected override void Seed(SchoolContext context)
        {
            var students = new List<Student>
            {
                new Student { FirstMidName="Carson", LastName="Alexander", EnrollmentDate=DateTime.Parse("2005-09-01") },
                new Student { FirstMidName="Meredith", LastName="Alonso", EnrollmentDate=DateTime.Parse("2002-09-01") },
                new Student { FirstMidName="Arturo", LastName="Anand", EnrollmentDate=DateTime.Parse("2003-09-01") },
                new Student { FirstMidName="Gytis", LastName="Barzdukas", EnrollmentDate=DateTime.Parse("2002-09-01") },
                new Student { FirstMidName="Yan", LastName="Li", EnrollmentDate=DateTime.Parse("2002-09-01") },
                new Student { FirstMidName="Peggy", LastName="Justice", EnrollmentDate=DateTime.Parse("2001-09-01") },
                new Student { FirstMidName="Laura", LastName="Norman", EnrollmentDate=DateTime.Parse("2003-09-01") },
                new Student { FirstMidName="Nino", LastName="Olivetto", EnrollmentDate=DateTime.Parse("2005-09-01") }
            };

            students.ForEach(s => context.Students.Add(s));
            context.SaveChanges();

            var courses = new List<Course>
            {
                new Course { CourseID=1050, Title="Chemistry", Credits=3, },
                new Course { CourseID=4022, Title="Micro economics", Credits=3,  },
                new Course { CourseID=4041, Title="Macro economics", Credits=3,  },
                new Course { CourseID=1045, Title="Calculus", Credits=4, },
                new Course { CourseID=3141, Title="Trigonometry", Credits=4, },
                new Course { CourseID=2021, Title="Composition", Credits=3 },
                new Course { CourseID=2042, Title="Literature", Credits=4 }
            };
            courses.ForEach(c => context.Courses.Add(c));
            context.SaveChanges();

            var enrollements = new List<Enrollment>
            {
                new Enrollment { StudentID=1, CourseID=1050, Grade=Grade.A },
                new Enrollment { StudentID=1, CourseID=4022, Grade= Grade.C },
                new Enrollment { StudentID=1, CourseID=4041, Grade= Grade.B },
                new Enrollment { StudentID=2, CourseID=1045, Grade=Grade.B },
                new Enrollment { StudentID=2, CourseID=3141, Grade=Grade.F},
                new Enrollment { StudentID=2, CourseID=2021, Grade=Grade.F },
                new Enrollment { StudentID=3, CourseID=1050 },
                new Enrollment { StudentID=4, CourseID=1050 },
                new Enrollment { StudentID=4, CourseID=4022, Grade= Grade.F },
                new Enrollment { StudentID=5, CourseID=4041, Grade= Grade.C },
                new Enrollment { StudentID=6, CourseID=1045 },
                new Enrollment { StudentID=7, CourseID=3441, Grade=Grade.A },
            };
            enrollements.ForEach(e => context.Enrollments.Add(e));
            context.SaveChanges();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>

  <connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=ContosoUniversity1; Integrated Security=SSPI;"  providerName="System.Data.SqlClient" />
  </connectionStrings>

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
  </system.webServer>


  <entityFramework>
    <contexts>
      <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
        <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity"></databaseInitializer>
      </context>

    </contexts>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>


</configuration>