ASP 核心迁移 1.1 到 2.0:Controller/Views 构建多个异常

ASP Core migration 1.1 to 2.0 : Controller/Views scaffolding multiple exceptions

我成功地将我的 asp 核心 1.1 项目迁移到 2.0,我可以毫无问题地构建和启动调试。

但是在使用脚手架生成控制器和视图时(VS Community 2017) 添加 > 添加脚手架项 > 带视图的 MVC 控制器,使用 Entity Framework

我在任何 POCO 上启动进程时遇到几个异常,例如我选择了第一个抛出的异常 => 我更改了默认模板以匹配我的模式(即每个控制器中通用服务层的 DI)和迁移前的项目(在 asp 核心 1.1 上)一切正常。

There was an error running the template C:\Users\Nicolas\Documents\Visual Studio 
2017\Projects\WebPortal\WebPortal\Templates\ControllerGenerator\MvcControllerWithContext.cshtml: Template Processing 
Failed:Template(45,31): error CS0246: The type or namespace name 'List<>' could not be found (are you missing a using 
directive or an assembly reference?) Template(65,33): error CS0246: The type or namespace name 'Dictionary<,>' could 
not be found (are you missing a using directive or an assembly reference?) Template(118,28): error CS0246: The type or
 namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?) at 
Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0() at 
Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at 
Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) at 
Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

我的 Controller 模板的相关行起诉脚手架:

var modelProperties = new List(); (line 45)
var relatedProperties = new Dictionary<string, dynamic>(); (line 65)
var dependencies = new List(); (line 118)

显然我为List和Dictionary导入了相应的命名空间。 如果我删除这些行,我会遇到其他异常,当项目在 .net core 1.1 版本上时不会抛出这些异常。

这是2.0项目的.csproj(脚手架失败)

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
  </PropertyGroup>

  <PropertyGroup>
    <UserSecretsId>aspnet-WebPortal-ed2c59f1-3859-46e1-af11-xxxxx</UserSecretsId>
    <PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>
</Project>

以及 1.1 项目的 .csproj(脚手架使用相同的模板)

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
  </PropertyGroup>

  <PropertyGroup>
    <UserSecretsId>aspnet-WebPortal-ed2c59f1-3859-46e1-af11-xxxxx</UserSecretsId>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
  </ItemGroup>

</Project>

任何人都可以在这一点上提供帮助吗?我当然可以提供模板。 问候

我终于设法让我的模板在 .net core 2.0 上运行,我检查了我的模板和 repo Templates Github repo 中的模板之间的差异,这里是强制性修改:

Templates\ControllerGenerator\MvcControllerWithContext.cshtml

添加:

@using System.Collections.Generic;
@using System.Linq;

另外:

using System.Collections.Generic;
using System.Linq;

所以 'header' 看起来像:

@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@using System.Collections.Generic;
@using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;

Templates\ViewGenerator\List.cshtml

添加:

@using System.Collections.Generic;
@using System.Linq;

所以 'header' 看起来像 :

@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@using Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore
@using System.Collections.Generic
@using System.Linq
@@model @getenumerabletypeexpression(Model.ViewDataTypeName)

然后进行如下替换:

IEnumerable<PropertyMetadata> properties = Model.ModelMetadata.Properties;

替换为

var properties = Model.ModelMetadata.Properties;

我不需要对我的其他视图模板进行任何更改,并且我不使用其他控制器模板