I am using EF4 Code First (CTP5) to generate my database and I am using DataAnnotations to try to control the generated DB schema. If I use the [Required] attribute, then the column is correctly generated as NOT NULL, but when I try to use the [StringLength(50)] attribute, all my columns end up getting created as NVarChar(max).
Here is a snippet of code for one of my clases (simplified) so you can see my problem:
public class Person
{
public int PersonID { get; set; }
// This property is generated as NVarChar(max) NOT NULL
// I prefer it be NVarChar(50) NOT NULL
[Required(ErrorMessage="Please enter a first name")]
[StringLength(50, ErrorMessage="First name is too long")]
public string FirstName { get; set; }
}
If the [StringLength] attribute isn’t working, is there another way to control the generated nvarchar column lengths?
Thanks,
Brent Stewart
I found that the issue I was experiencing was caused by a bug in LINQPad where it was not accuratly reporting the column types of my SQL Server CE 4 database. The [StringLength] Attribute worked exctly as it should.