none
How to attach data annotation with model class when working with EF database first RRS feed

  • Question

  • when we write model class by hand then we can attached data annotation with model class. here is a example

    public class Person
    {
      public int Id { get; set; }
      [Required]
      public string FirstName { get; set; }
      [Required]
      public string LastName { get; set; }
      public string Title { get; set; }
      public int Age { get; set; }
      public string Address { get; set; }
      public string City { get; set; }
      [Required]
      [Phone]
      public string Phone { get; set; }
      [Required]
      [EmailAddress]
      public string Email { get; set; }
    }

    now tell me when we work with EF database first approach which generate model class then how could we attach data annotation with model class. show me with details instruction and images. thanks

    Wednesday, December 6, 2017 1:58 PM

Answers

  • Hi Sudip_inn,

    >>now tell me when we work with EF database first approach which generate model class then how could we attach data annotation with model class. show me with details instruction and images. thanks

    You could add a metadata class that contains the attributes to achieve it.

    1. In the Models folder, add a class named Metadata.cs, replace the code in Metadata.cs with the following code.

    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace YourNamespace.Models
    {
        public class PersonMetadata
        {
           [Required]
           public string FirstName { get; set; }
    
           [Required]
           public string LastName { get; set; }
    
           [Required]
           [Phone]
           public string Phone { get; set; }
    
           [Required]
           [EmailAddress]
           public string Email { get; set; }    
        }
    
        // Other MetaDataClass
    }


    2. In the Models folder, add a class named PartialClasses.cs. Replace the contents of the file with the following code

    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace YourNamespace.Models
    {
        [MetadataType(typeof(PersonMetadata))]
        public partial class Person
        {
        }
    
        //Other Class
    
    }

    For more information, please refer to:

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, December 7, 2017 12:28 PM
    Thursday, December 7, 2017 2:05 AM
    Moderator

All replies

  • Hi Sudip_inn,

    >>now tell me when we work with EF database first approach which generate model class then how could we attach data annotation with model class. show me with details instruction and images. thanks

    You could add a metadata class that contains the attributes to achieve it.

    1. In the Models folder, add a class named Metadata.cs, replace the code in Metadata.cs with the following code.

    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace YourNamespace.Models
    {
        public class PersonMetadata
        {
           [Required]
           public string FirstName { get; set; }
    
           [Required]
           public string LastName { get; set; }
    
           [Required]
           [Phone]
           public string Phone { get; set; }
    
           [Required]
           [EmailAddress]
           public string Email { get; set; }    
        }
    
        // Other MetaDataClass
    }


    2. In the Models folder, add a class named PartialClasses.cs. Replace the contents of the file with the following code

    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace YourNamespace.Models
    {
        [MetadataType(typeof(PersonMetadata))]
        public partial class Person
        {
        }
    
        //Other Class
    
    }

    For more information, please refer to:

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, December 7, 2017 12:28 PM
    Thursday, December 7, 2017 2:05 AM
    Moderator
  • i found few good links https://ryanhayes.net/data-annotations-for-entity-framework-4-entities-as-an-mvc-model/

    https://www.red-gate.com/simple-talk/dotnet/net-framework/using-entity-framework-with-an-existing-database-data-access/

    https://eliot-jones.com/2014/8/metadata-ef-database-first

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation

    Thursday, December 7, 2017 12:49 PM
  • Hmm?

    Seems like I posted one of the to you.

    Thursday, December 7, 2017 3:55 PM