locked
EF 4.3 Code First and DataAnnotations RRS feed

  • Question

  • I am just getting my feet wet with the code first approach, and I got the impression that the generated database would respect the data annotations I added to my model properties.

    I tried adding [Required] on a string property, and I expected to get a non-null column.

    I tried adding [MaxLength(20)] on a string property, and I expected to get a column with this length.

    	public class BackendUser
    	{
    		public int BackendUserId { get; set; }
    
    		[Required]
    		[Display(Name = "User name")]
    		[MaxLength(20)]
    		public string UserName { get; set; }
    
    		[Required]
    		[DataType(DataType.Password)]
    		public string Password { get; set; }
    	}

    I ran the "Enable-Migrations" command from the package manager, and it generated an initial script, but I do not see these annotations being respected in this script.

                CreateTable(
                    "BackendUsers",
                    c => new
                        {
                            BackendUserId = c.Int(nullable: false, identity: true),
                            UserName = c.String(),
                            Password = c.String(),
                        })
                    .PrimaryKey(t => t.BackendUserId);

    Am I missing something here?

    Thanks a lot in advance

    Tuesday, March 27, 2012 8:44 AM

Answers

All replies