ASP.NET Core MetaDataType Attribute not working

半腔热情 提交于 2019-11-27 05:15:10
Guilherme Duarte

ASP.NET Core uses

Microsoft.AspNetCore.Mvc.Core.**ModelMetadataType** 

instead of

System.ComponentModel.DataAnnotations.**MetadataType** 

source

Try changing your attribute to [ModelMetadataType(typeof(ComponentModelMetaData))]

Another way... use same namespace

public class ApirolesMetaData
{
    [Required]
    public string Name { get; set; }
}


[ModelMetadataType(typeof(ApirolesMetaData))]
public partial class Apiroles
{

}

This is how I solved the same issue, I hope this solve your problem.

Entity class:

namespace CoreProject.Persistence.EFCore
{
    public partial class User
    {
        public User()
        {
            Reader = new HashSet<Reader>();
            Writer = new HashSet<Writer>();
        }

        public int UserId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string PasswordHashKey { get; set; }
        public byte Role { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime CreatedUtc { get; set; }
        public DateTime LastUpdateUtc { get; set; }
        public byte Status { get; set; }
        public bool Deleted { get; set; }
        public DateTime? ActivatedUtc { get; set; }
        public bool Test { get; set; }

        public virtual ICollection<Reader> Reader { get; set; }
        public virtual ICollection<Writer> Writer { get; set; }
    }
}

Metadata:

namespace CoreProject.Persistence.EFCore
{
    [ModelMetadataType(typeof(IUserMetadata))]
    public partial class User : IUserMetadata
    {
        public string FullName => FirstName + " " + LastName;
    }

    public interface IUserMetadata
    {
        [JsonProperty(PropertyName = "Id")]
        int UserId { get; set; }

        [JsonIgnore]
        string Password { get; set; }
        [JsonIgnore]
        string PasswordHashKey { get; set; }
        [JsonIgnore]
        byte Role { get; set; }
    }
}

Good luck...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!