Kevin S. Miller Asked: 2025-02-05 00:28:55 +0800 CST2025-02-05 00:28:55 +0800 CST 2025-02-05 00:28:55 +0800 CST EF Core CRUD Scaffold,在视图中显示主键吗? 772 当使用 EF Core DB 优先方法时,默认视图不显示主键。如何控制视图中显示哪些数据? 我搜索了模板和生成的代码。我搜索了 EF Core 教程和示例。 asp.net-core 2 个回答 Voted Best Answer Ruikai Feng 2025-02-05T14:23:04+08:002025-02-05T14:23:04+08:00 它使用文件夹中的模板C:\Users\{you}\.nuget\packages\microsoft.visualstudio.web.codegenerators.mvc\{version}\Templates\ViewGenerator 来搭建视图 列表模板: @inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase @using Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore @using System.Collections.Generic @using System.Linq @@model @GetEnumerableTypeExpression(Model.ViewDataTypeName) @{ if (Model.IsPartialView) { } else if (Model.IsLayoutPageSelected) { @:@@{ @:ViewData["Title"] = "@Model.ViewName"; if (!string.IsNullOrEmpty(Model.LayoutPageFile)) { @:Layout = "@Model.LayoutPageFile"; } @:} @: @:<h1>@Model.ViewName</h1> @: } else { @:@@{ @:Layout = null; @:} @: @:<!DOCTYPE html> @: @:<html> @:<head> @:<meta name="viewport" content="width=device-width" /> @:<title>@Model.ViewName</title> @:</head> @:<body> // PushIndent(" "); } @:<p> @:<a asp-action="Create">Create New</a> @:</p> @:<table class="table"> @:<thead> @:<tr> Dictionary<string, IPropertyMetadata> propertyLookup = ((IModelMetadata)Model.ModelMetadata).Properties.ToDictionary(x => x.PropertyName, x => x); Dictionary<string, INavigationMetadata> navigationLookup = ((IModelMetadata)Model.ModelMetadata).Navigations.ToDictionary(x => x.AssociationPropertyName, x => x); foreach (var item in Model.ModelMetadata.ModelType.GetProperties()) { if (propertyLookup.TryGetValue(item.Name, out IPropertyMetadata property) && property.Scaffold && !property.IsForeignKey && !property.IsPrimaryKey) { <th> @@Html.DisplayNameFor(model => model.@GetValueExpression(property)) </th> } else if (navigationLookup.TryGetValue(item.Name, out INavigationMetadata navigation)) { <th> @@Html.DisplayNameFor(model => model.@GetValueExpression(navigation)) </th> } } @:<th></th> @:</tr> @:</thead> @:<tbody> @:@@foreach (var item in Model) { @:<tr> foreach (var item in Model.ModelMetadata.ModelType.GetProperties()) { if (propertyLookup.TryGetValue(item.Name, out IPropertyMetadata property) && property.Scaffold && !property.IsForeignKey && !property.IsPrimaryKey) { <td> @@Html.DisplayFor(modelItem => item.@GetValueExpression(property)) </td> } else if (navigationLookup.TryGetValue(item.Name, out INavigationMetadata navigation)) { <td> @@Html.DisplayFor(modelItem => item.@GetValueExpression(navigation)[email protected]) </td> } } string pkName = GetPrimaryKeyName(); if (pkName != null) { @:<td> @:<a asp-action="Edit" asp-route-id="@@item.@pkName">Edit</a> | @:<a asp-action="Details" asp-route-id="@@item.@pkName">Details</a> | @:<a asp-action="Delete" asp-route-id="@@item.@pkName">Delete</a> @:</td> } else { <td> @@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | @@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | @@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) </td> } @:</tr> @:} @:</tbody> @:</table> if(!Model.IsPartialView && !Model.IsLayoutPageSelected) { //ClearIndent(); @:</body> @:</html> } } @functions { string GetPrimaryKeyName() { return (Model.ModelMetadata.PrimaryKeys != null && Model.ModelMetadata.PrimaryKeys.Length == 1) ? Model.ModelMetadata.PrimaryKeys[0].PropertyName : null; } string GetValueExpression(IPropertyMetadata property) { return property.PropertyName; } string GetValueExpression(INavigationMetadata navigation) { return navigation.AssociationPropertyName; } string GetEnumerableTypeExpression(string typeName) { return "IEnumerable<" + typeName + ">"; } } 如果要搭建显示主键的视图,请删除上面代码中的主键检查 && !property.IsPrimaryKey 结果: Ricardo Peres 2025-02-05T17:42:54+08:002025-02-05T17:42:54+08:00 还有EF Core Power Tools,你也有源代码。这是一个著名的项目,我认为你可以为此进行配置。
它使用文件夹中的模板
C:\Users\{you}\.nuget\packages\microsoft.visualstudio.web.codegenerators.mvc\{version}\Templates\ViewGenerator
来搭建视图列表模板:
如果要搭建显示主键的视图,请删除上面代码中的主键检查
结果:
还有EF Core Power Tools,你也有源代码。这是一个著名的项目,我认为你可以为此进行配置。