Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,198 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.