EF Core 7 和 ASP.NET Core MVC razor 视图不使用部分类中的元数据属性

问题描述 投票:0回答:1

我必须假设这是一个错误,因为我找不到任何解决方案。我在专用于数据访问的程序集中有 EF Core 7 脚手架部分类。

这是生成的:

namespace EF.DataAccessLayer;

public partial class ServerComponentConfiguration
{
    public int ServerComponent { get; set; }

    public string Element { get; set; } = null!;
    public string Property { get; set; } = null!;
    public string Value { get; set; } = null!;

    public DateTime? LastUpdated { get; set; }

    public virtual ServerComponent ServerComponentNavigation { get; set; } = null!;
}

这是我在同一程序集中的单独文件中添加的元数据:

namespace EF.DataAccessLayer;

[MetadataType(typeof(ServerComponentConfigurationMetadata))]
public partial class ServerComponentConfiguration 
{
}

public class ServerComponentConfigurationMetadata 
{
    [Display(Name = "Last Update")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime? LastUpdated { get; set; }
}

我在控制器中使用了这段代码,以便在运行时查看部分类中的属性是否已合并到 EF 生成的

ServerComponentConfiguration
类中:

var metadataTypeAttribute = typeof(ServerComponentConfiguration).GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault() as MetadataTypeAttribute;

if (metadataTypeAttribute != null) 
{
    var metadataClassProperties = metadataTypeAttribute.MetadataClassType.GetProperties();

    foreach (var prop in metadataClassProperties) 
    {
        var displayAttributes = prop.GetCustomAttributes(typeof(DisplayAttribute), true);
        var displayFormatAttributes = prop.GetCustomAttributes(typeof(DisplayFormatAttribute), true);
    }
}

这是 Razor 视图,此声明位于顶部:

@using EF.DataAccessLayer;
@model IEnumerable<ServerComponentConfiguration>

此视图看不到或使用属性。

编辑:为了完整性,我正在使用

@Html.DisplayNameFor(model => model.LastUpdated)

@Html.DisplayFor(modelItem => item.LastUpdated)

作为解决方法,我创建了一个类,该类复制整个生成的 EF 类,在其中添加属性,用不必要的重复填充值,然后它就可以工作了。所以我唯一能想到的是属性的实现方式是罪魁祸首;但我不知道如何纠正这个问题。

c# entity-framework-core asp.net-core-mvc .net-7.0
1个回答
0
投票

ModelMetadataType 属性可以指定与数据模型类关联的元数据类,您可以尝试将该属性更改为:

[ModelMetadataType(typeof(ServerComponentConfigurationMetadata))]

以下是您可以用作参考的示例: 我的型号:

public partial class ServerComponentConfiguration
{
    public int ServerComponent { get; set; }

    public string Element { get; set; } = null!;
    public string Property { get; set; } = null!;
    public string Value { get; set; } = null!;

    public DateTime? LastUpdated { get; set; }


}

[ModelMetadataType(typeof(ServerComponentConfigurationMetadata))]
public partial class ServerComponentConfiguration
{
}

public partial class ServerComponentConfigurationMetadata
{
    [Display(Name = "Last Update")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime? LastUpdated { get; set; }
}

我的控制器:

public IActionResult Index()
{           
     ServerComponentConfiguration model = new ServerComponentConfiguration
     {
         LastUpdated = DateTime.Today
                
     };

     return View(model);
}

我的页面:

@using Dropdowm.Models;
@model ServerComponentConfiguration

<p>Last Updated: @Html.DisplayFor(model => model.LastUpdated)</p>

这是使用

[ModelMetadataType(typeof(ServerComponentConfigurationMetadata))]
的结果:

这是使用的结果

[MetadataType(typeof(ServerComponentConfigurationMetadata))]

© www.soinside.com 2019 - 2024. All rights reserved.