Posts

Showing posts from July, 2012

Entity Framework 4.3 Seeding Data Using int as Identity Column Type

When seeding data using EF 4.3 code first, there is a gotcha when seeding data in identity columns. Let me explain.


In a recent project, I had two tables as part of my model. The first one was called Questions, and the code looked like:

[Table("Questions", Schema = "Forms")] public class Question { public int QuestionID { get; set; } [Required(ErrorMessage = "The question is required")] [MaxLength(400)] public string QuestionText { get; set; } public string Marker { get; set; } }
The next table was a relationship table to link Question records to a table with Form records. I had used a linking table as I needed a display order column:

[Table("FormsQuestions", Schema = "Forms")] public class FormsQuestions { [Key, ForeignKey("Form"), Column(Order = 0)] public int FormID { get; set; } public virtual Form Form { get; set; } …