Subversion Repository Public Repository

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
@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;
@{
    foreach (var namespaceName in Model.RequiredNamespaces)
    {
@:using @namespaceName;
    }
}

namespace @Model.ControllerNamespace
{
@{
    string routePrefix;
    if (String.IsNullOrEmpty(Model.AreaName))
    {
        routePrefix = Model.ControllerRootName;
    }
    else
    {
        routePrefix = Model.AreaName + "/" + Model.ControllerRootName;
    }
    var modelProperties = new List<string>();
    foreach (var property in Model.ModelMetadata.Properties)
    {
        if (property.Scaffold)
        {
            modelProperties.Add(property.PropertyName);
        }
    }
    var bindString = string.Join(",", modelProperties);
    var contextTypeName = Model.ContextTypeName;
    var entitySetName = Model.ModelMetadata.EntitySetName;
    var entitySetVar = Model.EntitySetVariable ??
        (String.IsNullOrEmpty(entitySetName)
            ? entitySetName
            : (entitySetName.Substring(0, length: 1).ToLowerInvariant() + entitySetName.Substring(1)));
    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);
    }

    if (!string.IsNullOrEmpty(Model.AreaName))
    {
    @:@string.Format("[Area(\"{0}\")]", Model.AreaName)
    }
}
    public class @Model.ControllerName : Controller
    {
        private readonly @Model.ContextTypeName _context;

        public @(Model.ControllerName)(@Model.ContextTypeName context)
        {
            _context = context;
        }

        // GET: @routePrefix
@{
        @:public async Task<IActionResult> Index()
        @:{
    var includeExpressions = "";
    includeExpressions = String.Join("", relatedProperties
        .Values
        .Select(property => String.Format(".Include({0} => {0}.{1})", lambdaVar, property.AssociationPropertyName)));
    if (!String.IsNullOrEmpty(includeExpressions))
    {
            @:var @entitySetVar = _context.@entitySetName@includeExpressions;
            @:return View(await @(entitySetVar).ToListAsync());
    }
    else
    {
            @:return View(await _context.@(entitySetName).ToListAsync());
    }
}        }

        // GET: @routePrefix/Details/5
        public async Task<IActionResult> Details(@primaryKeyNullableTypeName id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var @Model.ModelVariable = await _context.@(entitySetName)@inlineIncludes
                .FirstOrDefaultAsync(m => m.@primaryKeyName == id);
            if (@Model.ModelVariable == null)
            {
                return NotFound();
            }

            return View(@Model.ModelVariable);
        }

        // GET: @routePrefix/Create
        public IActionResult Create()
        {
@{
    foreach (var property in relatedProperties.Values)
    {
            @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName");
    }
}            return View();
        }

        // POST: @routePrefix/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("@bindString")] @Model.ModelTypeName @Model.ModelVariable)
        {
            if (ModelState.IsValid)
            {
@{
    if (!string.IsNullOrEmpty(primaryKeyType) && IsGuid(primaryKeyType))
    {
                @:@(Model.ModelVariable).@primaryKeyName = Guid.NewGuid();
    }
                @:_context.Add(@Model.ModelVariable);
                @:await _context.SaveChangesAsync();
}                return RedirectToAction(nameof(Index));
            }
@{
    foreach (var property in relatedProperties.Values)
    {
            @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName", @(Model.ModelVariable).@property.ForeignKeyPropertyNames[0]);
    }
}
            return View(@Model.ModelVariable);
        }

        // GET: @routePrefix/Edit/5
        public async Task<IActionResult> Edit(@primaryKeyNullableTypeName id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var @Model.ModelVariable = await _context.@(entitySetName).FindAsync(id);
            if (@Model.ModelVariable == null)
            {
                return NotFound();
            }
@{
    foreach (var property in relatedProperties.Values)
    {
            @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName", @(Model.ModelVariable).@property.ForeignKeyPropertyNames[0]);
    }
}
            return View(@Model.ModelVariable);
        }

        // POST: @routePrefix/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(@primaryKeyShortTypeName id, [Bind("@bindString")] @Model.ModelTypeName @Model.ModelVariable)
        {
            if (id != @Model.ModelVariable.@primaryKeyName)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(@Model.ModelVariable);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!@(Model.ModelTypeName)Exists(@Model.ModelVariable.@primaryKeyName))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
@{
    foreach (var property in relatedProperties.Values)
    {
            @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName", @(Model.ModelVariable).@property.ForeignKeyPropertyNames[0]);
    }
}
            return View(@Model.ModelVariable);
        }

        // GET: @routePrefix/Delete/5
        public async Task<IActionResult> Delete(@primaryKeyNullableTypeName id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var @Model.ModelVariable = await _context.@(entitySetName)@inlineIncludes
                .FirstOrDefaultAsync(m => m.@primaryKeyName == id);
            if (@Model.ModelVariable == null)
            {
                return NotFound();
            }

            return View(@Model.ModelVariable);
        }

        // POST: @routePrefix/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(@primaryKeyShortTypeName id)
        {
            var @Model.ModelVariable = await _context.@(entitySetName).FindAsync(id);
            _context.@(entitySetName).Remove(@Model.ModelVariable);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool @(Model.ModelTypeName)Exists(@primaryKeyShortTypeName id)
        {
            return _context.@(entitySetName).Any(e => e.@primaryKeyName == id);
        }
    }
}
@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);
    }
}

Commits for ChrisCompleteCodeTrunk/ActionTireCo/packages/Microsoft.VisualStudio.Web.CodeGenerators.Mvc.2.1.0/Templates/ControllerGenerator/MvcControllerWithContext.cshtml

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000