ASP核心迁移1.1到2.0:控制器/视图脚手架多个异常

时间:2017-09-06 07:35:33

标签: scaffolding asp.net-core-2.0

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

但是在使用脚手架生成控制器和视图时(VS社区2017) 添加>添加脚手架项>带有视图的MVC控制器,使用实体框架

在任何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)

我的控制器模板的相关部分起诉了脚手架:

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

显然我为List和Dictionary导入了相应的命名空间。 如果我删除这些行,我有其他例外,当项目在.net核心1.1版本时没有抛出。

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

<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>

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

<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>

任何人都可以提供帮助吗?我可以提供模板。 此致

1 个答案:

答案 0 :(得分:0)

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

<强>模板\ ControllerGenerator \ MvcControllerWithContext.cshtml

添加:

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

另外:

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

所以'标题'看起来像:

@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;

<强>模板\ ViewGenerator \ List.cshtml

添加:

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

所以'标题'看起来像:

@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;

我没有必要将其他任何内容更改为其他视图模板,而且我不使用其他控制器模板