Hi,
I recently migrate from core 1.1 to core 2.0 on my ASP web project.
I managed to make it work fine except for the Scaffold part, I personnalized the supplied templates for my 1.1 core project (they did work fine), but on 2.0 I get multiple errors.
Here I use 'MVC Controller with views, using Entity Framework' on a simple POCO.
First problem is that some namespace are not resolved :
Here is the exception thrown on scaffold creation :
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 :
var modelProperties = new List(); (line 45) var relatedProperties = new Dictionary<string, dynamic>(); (line 65) var dependencies = new List(); (line 118)
but I did use the adhoc namespace :
using System.Collections.Generic;
If I change these lines to :
var modelProperties = new System.Collections.Generic.List<string>(); (line 45) var relatedProperties = new System.Collections.Generic.Dictionary<string, dynamic>(); (line 65) var dependencies = new System.Collections.Generic.List<string>(); (line 118)
Then the error goes, but I have another exception:
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(116,10): error CS1061: 'Dictionary<string, dynamic>.ValueCollection' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'Dictionary<string, dynamic>.ValueCollection' could 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 :
var includeExpressions = ""; includeExpressions = String.Join("", relatedProperties .Values .Select(property => String.Format(".Include({0} => {0}.{1})", lambdaVar, property.AssociationPropertyName)));
I don't understand why this error is thrown, but If I comment out these lines (here I loose functionnality), the scaffold of the Controller part passes, but errors are now on scaffold Views :
There was an error running the template C:\Users\Nicolas\Documents\Visual Studio 2017\Projects\WebPortal\WebPortal\Templates\ViewGenerator\List.cshtml: Template Processing Failed:Template(62,5): error CS0246: The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?) Template(63,30): error CS1579: foreach statement cannot operate on variables of type 'IEnumerable<PropertyMetadata>' because 'IEnumerable<PropertyMetadata>' does not contain a public definition for 'GetEnumerator' Template(85,47): error CS1579: foreach statement cannot operate on variables of type 'IEnumerable<PropertyMetadata>' because 'IEnumerable<PropertyMetadata>' does not contain a public definition for 'GetEnumerator' 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)
Here are the concerned lines :
IEnumerable<PropertyMetadata> properties = Model.ModelMetadata.Properties; (line 62) foreach (var property in properties) (line 63) { if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey) {<th> @@Html.DisplayNameFor(model => model.@GetValueExpression(property)) </th> } } foreach (var navigation in Model.ModelMetadata.Navigations) { <th> @@Html.DisplayNameFor(model => model.@GetValueExpression(navigation)) </th> }
I really don't know what happen, could you help me ?
Best Regards
Nicolas