ChrisCompleteCodeTrunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
@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.RazorPages;
using Microsoft.EntityFrameworkCore;
@{
var entitySetName = Model.ModelMetadata.EntitySetName;
var primaryKeyName = Model.ModelMetadata.PrimaryKeys[0].PropertyName;
var primaryKeyShortTypeName = Model.ModelMetadata.PrimaryKeys[0].ShortTypeName;
var primaryKeyType = Model.ModelMetadata.PrimaryKeys[0].TypeName;
var primaryKeyNullableTypeName = GetNullableTypeName(primaryKeyType, primaryKeyShortTypeName);
var lambdaVar = Model.ModelVariable[0];
var relatedProperties = new Dictionary<string, dynamic>();
foreach (var nav in Model.ModelMetadata.Navigations)
{
relatedProperties.Add(nav.AssociationPropertyName, nav);
}
var inlineIncludes = "";
foreach (var property in relatedProperties.Values)
{
inlineIncludes += string.Format("{0} .Include({1} => {1}.{2})", Environment.NewLine, lambdaVar, property.AssociationPropertyName);
}
foreach (var namespaceName in Model.RequiredNamespaces)
{
@:using @namespaceName;
}
}
namespace @Model.NamespaceName
{
public class @Model.PageModelClassName : PageModel
{
private readonly @Model.ContextTypeName _context;
public @(Model.PageModelClassName)(@Model.ContextTypeName context)
{
_context = context;
}
public IList<@Model.ViewDataTypeShortName> @Model.ViewDataTypeShortName { get;set; }
public async Task OnGetAsync()
{
@Model.ViewDataTypeShortName = await _context.@(entitySetName)@(inlineIncludes).ToListAsync();
}
}
}
@functions
{
// This function converts the primary key short type name to its nullable equivalent when possible. This is required to make
// sure that an HTTP 400 error is thrown when the user tries to access the edit, delete, or details action with null values.
string GetNullableTypeName(string typeName, string shortTypeName)
{
// The exceptions are caught because if for any reason the type is user defined, then the short type name will be used.
// In that case the user will receive a server error if null is passed to the edit, delete, or details actions.
Type primaryKeyType = null;
try
{
primaryKeyType = Type.GetType(typeName);
}
catch
{
}
if (primaryKeyType != null && (!Microsoft.VisualStudio.Web.CodeGeneration.Templating.TypeUtilities.IsNullable(primaryKeyType) || IsGuid(typeName)))
{
return shortTypeName + "?";
}
return shortTypeName;
}
bool IsGuid(string typeName) {
return String.Equals("System.Guid", typeName, StringComparison.OrdinalIgnoreCase);
}
}
|
Revision |
Author |
Commited |
Message |
1
|
BBDSCHRIS
|
Wed 22 Aug, 2018 20:08:03 +0000 |
|