Here's a model class (generated by scaffolding, nothing changed):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace EFDBFirst.Models
{
public partial class Product
{
public Product()
{
OrderDetails = new HashSet<OrderDetail>();
}
public int ProductId { get; set; }
public string ProductName { get; set; } = null!;
public int? SupplierId { get; set; }
public int? CategoryId { get; set; }
public string? QuantityPerUnit { get; set; }
public decimal? UnitPrice { get; set; }
public short? UnitsInStock { get; set; }
public short? UnitsOnOrder { get; set; }
public short? ReorderLevel { get; set; }
public bool Discontinued { get; set; }
public virtual Category? Category { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}
}
In order to preserve data annotation attributes after running scaffold-dbcontext
, I assumed I would
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
namespace EFDBFirst.Models
{
public partial class ProductMetadata
{
[Required(ErrorMessage = @"Qty/Unit is required")]
[Display(Name ="QPU")]
public string? QuantityPerUnit { get; set; }
}
}
Product
class where I link the metadata class:using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace EFDBFirst.Models
{
[MetadataType(typeof(ProductMetadata))]
public partial class Product
{
}
}
But the display name doesn't show, and the required attribute isn't enforced.
Things I've already done:
BTW, if I add the attribute directly into the original model class, everything works as expected.
Any ideas? I can't for the life of me figure out what's (not) going on here....
MetadataType
is outdated, and we shall use ModelMetadataType
instead. Get OP's confirmation that it can solve the issue.