EF Core Data Attributes are not overwritten by ModelMetadataType attributes

Daniel 21 Reputation points
2022-05-06T15:23:27.083+00:00

I have an EF Core partial class from our database table that includes data annotations, such as Required.

I also have a Metadata class and another partial that as far as I can tell 'should' be overwriting those EF annotations, but that is not happening.

snippet of EF class:

namespace Business  
{  
    public partial class Staff  
    {  
        [Key]  
        public int ID { get; set; }  
        [Required]  
        [StringLength(50)]  
        public string FirstName { get; set; }  
        [Required]  
        [StringLength(50)]  
        public string LastName { get; set; }  
    }  
}  

snippet of Metadata class:

namespace Business  
{  
    /// <summary>  
    /// The Staff object  
    /// </summary>  
    public class StaffMD  
    {  
        [DisplayName("ID")]  
        [Description("The unique key for a Staff")]  
        [Required(ErrorMessage = "ID is required")]  
        public int ID { get; set; }  
        [DisplayName("First Name")]  
        [Description("The First Name for a Staff")]  
        [Required(ErrorMessage = "First Name is required")]  
        [MaxLength(50, ErrorMessage = "First Name can only be {1} characters")]  
        public string FirstName { get; set; }  
        [DisplayName("Last Name")]  
        [Description("The Last Name for a Staff")]  
        [Required(ErrorMessage = "Last Name is required")]  
        [MaxLength(50, ErrorMessage = "Last Name can only be {1} characters")]  
        public string LastName { get; set; }  
    }  
  
    [ModelMetadataType(typeof(StaffMD))]  
    public partial class Staff  
    {  
    }  
}  

snippet of view:

@model Staff  
@using Business;  
  
@{  
    ViewData["Title"] = "Create - Staff";  
}  
  
@using (Html.BeginForm("Create", "Staff", null, FormMethod.Post, true, null))  
{  
    <p>  
        <div class="formContainer" id="formContainerParent">  
            <div class="formContainer-item">  
                <h2 class="formContainer-header" id="formHeadingOne">  
                    <label class="formContainer-headerContent">  
                        Create Staff  
                    </label>  
                </h2>  
                <div id="formContentOne" class="formContainer-body" aria-labelledby="formHeadingOne">  
                    <div class="formContainer-bodyContent">  
                        <div class="row">  
                            <div class="col-md-3">  
                                <div>  
                                    @Html.AccessibleRequiredLabelFor(model => model.FirstName, null, null, new { @class = "control-label" })  
                                </div>  
                                <div>  
                                    @Html.TextBoxFor(model => model.FirstName, null, new { @class = "form-control" })  
                                    @Html.ValidationMessageFor(model => model.FirstName)  
                                </div>  
                            </div>  
                            <div class="col-md-3">  
                                <div>  
                                    @Html.AccessibleRequiredLabelFor(model => model.LastName, null, null, new { @class = "control-label" })  
                                </div>  
                                <div>  
                                    @Html.TextBoxFor(model => model.LastName, null, new { @class = "form-control" })  
                                    @Html.ValidationMessageFor(model => model.LastName)  
                                </div>  
                            </div>  
                        </div>  
                    </div>  
                </div>  
            </div>  
        </div>  
    </p>  
}  

Image of application using 'default' Required message from EF core attribute
199719-image.png

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
725 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,386 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,096 Reputation points Microsoft Vendor
    2022-05-09T05:45:26.407+00:00

    Hi @Daniel ,

    The MetadataTypeAttribute attribute enables you to associate a class with a data-model partial class. In this associated class you provide additional metadata information that is not in the data model. So, if the data-model partial class (EF class) and associated class both sets the RequiredAttribute attribute, it will use the data-model partial class (EF class)'s RequiredAttribute attribute.

    So, to use the ModelMetadataType's validation message, you can remove the DataAnnotations from the EF class:

     namespace Business  
     {  
         public partial class Staff  
         {  
             [Key]  
             public int ID { get; set; }   
             public string FirstName { get; set; }   
             public string LastName { get; set; }  
         }  
     }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful