ASP.NET Core MetaDataType Attribute not workingAdd data annotations to a class generated by entity frameworkASP.NET Core MVC - Model Binding : Bind an interface model using the attribute [FromBody] (BodyModelBinder)ModelMetadataType has no effect on model in asp.net core 2.0Loading MetaData in Aspnet CoreCan I use Partial Classes to determine how data models get Json Serialized in .Net Core 2.0 APISeparating the serialization metadata from the implementation of a data contract?How to transfer DataAnnotations from model to viewModel in asp.net core?Why is Entity Framework data annotation not getting picked upWhat does the [Flags] Enum Attribute mean in C#?Most Useful AttributesFile Upload ASP.NET MVC 3.0.net mvc 2 validation: summing up values of several attributesHow does the StringLengthAttribute work?MVC change ModelBinder unit testMetadataType and DataAnnotations not WorkingValidation of a Dropdown which property comes from model but created from a RenderActionHow to handle DataAnnotation RequiredField(ErrorMessage…) using EntityTypeConfigurationASP Core MVC Scaffolding SelectList (RC1) not populating data

How to say in German "enjoying home comforts"

What is the word for reserving something for yourself before others do?

Is "remove commented out code" correct English?

I would say: "You are another teacher", but she is a woman and I am a man

What to put in ESTA if staying in US for a few days before going on to Canada

A reference to a well-known characterization of scattered compact spaces

Is it possible to run Internet Explorer on OS X El Capitan?

Is it inappropriate for a student to attend their mentor's dissertation defense?

Alternative to sending password over mail?

Has there ever been an airliner design involving reducing generator load by installing solar panels?

Can I use a neutral wire from another outlet to repair a broken neutral?

Neighboring nodes in the network

Were any external disk drives stacked vertically?

Should I tell management that I intend to leave due to bad software development practices?

Is there a hemisphere-neutral way of specifying a season?

Twin primes whose sum is a cube

Took a trip to a parallel universe, need help deciphering

Why is consensus so controversial in Britain?

Stopping power of mountain vs road bike

Brothers & sisters

Is Lorentz symmetry broken if SUSY is broken?

How can I tell someone that I want to be his or her friend?

Anagram holiday

What is the most common color to indicate the input-field is disabled?



ASP.NET Core MetaDataType Attribute not working


Add data annotations to a class generated by entity frameworkASP.NET Core MVC - Model Binding : Bind an interface model using the attribute [FromBody] (BodyModelBinder)ModelMetadataType has no effect on model in asp.net core 2.0Loading MetaData in Aspnet CoreCan I use Partial Classes to determine how data models get Json Serialized in .Net Core 2.0 APISeparating the serialization metadata from the implementation of a data contract?How to transfer DataAnnotations from model to viewModel in asp.net core?Why is Entity Framework data annotation not getting picked upWhat does the [Flags] Enum Attribute mean in C#?Most Useful AttributesFile Upload ASP.NET MVC 3.0.net mvc 2 validation: summing up values of several attributesHow does the StringLengthAttribute work?MVC change ModelBinder unit testMetadataType and DataAnnotations not WorkingValidation of a Dropdown which property comes from model but created from a RenderActionHow to handle DataAnnotation RequiredField(ErrorMessage…) using EntityTypeConfigurationASP Core MVC Scaffolding SelectList (RC1) not populating data






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








16















I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set.
But it doesn't do as advertised. What is causing the issue 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;










share|improve this question






























    16















    I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set.
    But it doesn't do as advertised. What is causing the issue 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;










    share|improve this question


























      16












      16








      16


      1






      I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set.
      But it doesn't do as advertised. What is causing the issue 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;










      share|improve this question
















      I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set.
      But it doesn't do as advertised. What is causing the issue 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;







      c# asp.net-core data-annotations






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 7 '17 at 23:22









      Camilo Terevinto

      19.6k63969




      19.6k63969










      asked Jan 3 '16 at 13:15









      MetaMeta

      119110




      119110






















          2 Answers
          2






          active

          oldest

          votes


















          31














          ASP.NET Core uses



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


          instead of



          System.ComponentModel.DataAnnotations.**MetadataType** 


          source



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






          share|improve this answer




















          • 4





            My deepest thanks for finding Microsoft.AspNetCore.Mvc.ModelMetadataTypeAttribute for .NET Core 1.0 ... different name, different namespace ... not hard to find. Anyway, this is the correct answer, since the partial class Component appears to be the model declaration. I prefer to use a partial class named the same and in the same logical namespace as the model to apply Validate and the metadata's annotations to the model. That way, if I regenerate the model from the database, I don't lose my edits.

            – Gopher
            Aug 10 '16 at 14:52


















          1














          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...






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34576921%2fasp-net-core-metadatatype-attribute-not-working%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            31














            ASP.NET Core uses



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


            instead of



            System.ComponentModel.DataAnnotations.**MetadataType** 


            source



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






            share|improve this answer




















            • 4





              My deepest thanks for finding Microsoft.AspNetCore.Mvc.ModelMetadataTypeAttribute for .NET Core 1.0 ... different name, different namespace ... not hard to find. Anyway, this is the correct answer, since the partial class Component appears to be the model declaration. I prefer to use a partial class named the same and in the same logical namespace as the model to apply Validate and the metadata's annotations to the model. That way, if I regenerate the model from the database, I don't lose my edits.

              – Gopher
              Aug 10 '16 at 14:52















            31














            ASP.NET Core uses



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


            instead of



            System.ComponentModel.DataAnnotations.**MetadataType** 


            source



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






            share|improve this answer




















            • 4





              My deepest thanks for finding Microsoft.AspNetCore.Mvc.ModelMetadataTypeAttribute for .NET Core 1.0 ... different name, different namespace ... not hard to find. Anyway, this is the correct answer, since the partial class Component appears to be the model declaration. I prefer to use a partial class named the same and in the same logical namespace as the model to apply Validate and the metadata's annotations to the model. That way, if I regenerate the model from the database, I don't lose my edits.

              – Gopher
              Aug 10 '16 at 14:52













            31












            31








            31







            ASP.NET Core uses



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


            instead of



            System.ComponentModel.DataAnnotations.**MetadataType** 


            source



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






            share|improve this answer















            ASP.NET Core uses



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


            instead of



            System.ComponentModel.DataAnnotations.**MetadataType** 


            source



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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 7 '17 at 23:23









            Camilo Terevinto

            19.6k63969




            19.6k63969










            answered May 22 '16 at 15:07









            Guilherme DuarteGuilherme Duarte

            2,42112231




            2,42112231







            • 4





              My deepest thanks for finding Microsoft.AspNetCore.Mvc.ModelMetadataTypeAttribute for .NET Core 1.0 ... different name, different namespace ... not hard to find. Anyway, this is the correct answer, since the partial class Component appears to be the model declaration. I prefer to use a partial class named the same and in the same logical namespace as the model to apply Validate and the metadata's annotations to the model. That way, if I regenerate the model from the database, I don't lose my edits.

              – Gopher
              Aug 10 '16 at 14:52












            • 4





              My deepest thanks for finding Microsoft.AspNetCore.Mvc.ModelMetadataTypeAttribute for .NET Core 1.0 ... different name, different namespace ... not hard to find. Anyway, this is the correct answer, since the partial class Component appears to be the model declaration. I prefer to use a partial class named the same and in the same logical namespace as the model to apply Validate and the metadata's annotations to the model. That way, if I regenerate the model from the database, I don't lose my edits.

              – Gopher
              Aug 10 '16 at 14:52







            4




            4





            My deepest thanks for finding Microsoft.AspNetCore.Mvc.ModelMetadataTypeAttribute for .NET Core 1.0 ... different name, different namespace ... not hard to find. Anyway, this is the correct answer, since the partial class Component appears to be the model declaration. I prefer to use a partial class named the same and in the same logical namespace as the model to apply Validate and the metadata's annotations to the model. That way, if I regenerate the model from the database, I don't lose my edits.

            – Gopher
            Aug 10 '16 at 14:52





            My deepest thanks for finding Microsoft.AspNetCore.Mvc.ModelMetadataTypeAttribute for .NET Core 1.0 ... different name, different namespace ... not hard to find. Anyway, this is the correct answer, since the partial class Component appears to be the model declaration. I prefer to use a partial class named the same and in the same logical namespace as the model to apply Validate and the metadata's annotations to the model. That way, if I regenerate the model from the database, I don't lose my edits.

            – Gopher
            Aug 10 '16 at 14:52













            1














            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...






            share|improve this answer





























              1














              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...






              share|improve this answer



























                1












                1








                1







                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...






                share|improve this answer















                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...







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 8 at 0:04

























                answered Feb 24 at 12:57









                Javier ContrerasJavier Contreras

                114




                114



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34576921%2fasp-net-core-metadatatype-attribute-not-working%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                    Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                    Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved