Search code examples
scaffoldingasp.net-core-2.0

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


I migrate my asp core 1.1 project to 2.0 with success, I can build, and launch debug with no problem.

But when using the scaffolding to generates Controllers and Views (VS Community 2017) Add > Add Scaffolded item > MVC Controller with views, using Entity Framework

I've got several exceptions when launching the process on any POCO, as example I picked the first thrown exception => I changed the default templates to match my pattern (ie DI of generic Service Layer within each controller) and on the project before migration (on asp core 1.1) everything worked fine.

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)

And the concerned lines of my Controller template sued for the scaffold:

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

Obviously I import the corresponding namespace for List and Dictionary. If I remove these lines I've got other exceptions which were not thrown when the project was on .net core 1.1 version.

Here is the .csproj for 2.0 project (scaffolding fails)

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

And the .csproj for 1.1 project (scaffolding works with same template)

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

Anyone could help on that point ? I can supply the templates off course. Regards


Solution

  • I finally managed to make my template works on .net core 2.0, I checked the differences between my templates and the templates from the repo Templates Github repo, and here are the mandatory modifications :

    Templates\ControllerGenerator\MvcControllerWithContext.cshtml

    Add :

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

    in addition of :

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

    So the 'header' looks like :

    @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

    Add :

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

    so the 'header' looks like :

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

    Then make the following replacement :

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

    replaced by

    var properties = Model.ModelMetadata.Properties;
    

    I didn't have to change anything else to my other view templates, and I don't use other controller templates