ASP.NET Core MetaDataType attribute not working

I use the MetaDataType attribute in the class of my domain model. It must move the attribute information from the reference class to the class that was set by the MetadataType attribute. But this is not as advertised. What causes the problem here?

[MetadataType(typeof(ComponentModelMetaData))] public partial class Component { public int Id { get; set; } public string Name { get; set; } public ICollection<Repo> Repos { get; set; } public string Description { get; set; } } public class ComponentModelMetaData { [Required(ErrorMessage = "Name is required.")] [StringLength(30, MinimumLength = 3, ErrorMessage = "Name length should be more than 3 symbols.")] public string Name { get; set; } public ICollection<Repo> Repos { get; set; } [Required(ErrorMessage = "Description is required.")] public string Description { get; set; } } 
+20
c # asp.net-core data-annotations
Jan 03 '15 at 13:15
source share
3 answers

ASP.NET Core uses

 Microsoft.AspNetCore.Mvc **ModelMetadataType** 

instead

 System.ComponentModel.DataAnnotations.**MetadataType** 

source

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

+38
May 22 '16 at 15:07
source share

First an ASP.NET Core database, this works ...

+2
May 04 '19 at 12:41
source share

So I solved the same problem, I hope this solves 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 ...

+1
Feb 24 '19 at 12:57
source share



All Articles