C# Entity Framework Basics Extending Our Entity Data Model Using Data Annotations to Refine the Generated Database

Adding data attributes

Does it matter where you put the data attributes in the properties section of the Entity class?

namespace ComicBookGalleryModel.Models
{
    public class Artist
    {
        public Artist()
        {
            ComicBooks = new List<ComicBookArtist>();
        }

        public int Id { get; set; }
        [Required, StringLength(100)] // <-----  Instructor put it here above the Name 
        public string Name { get; set; }
       // Can it go here as well? 




        public ICollection<ComicBookArtist> ComicBooks { get; set; }
    }

Seems to me that you can and I would think for readability sake you would put it right after the Name property. If not why would you put it before the property?

1 Answer

Steven Parker
Steven Parker
171,242 Points

The attributes must go directly before the item that they apply to. This is a syntax requirement, just like the placement of the access specifier and the type.