Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Migrations not creating all fields from class

MVC 5

When creating a new migration it is generating the some tables correctly. However, classes like the one below are being generated with almost all of the fields missing in migration script.

 public class Meeting
    {
        public int ID { get; set; }

        [Display(Name = "Meeting Topic")]
        public string MeetingTopic;   

        [Display(Name = "Meeting Minutes")]
        public int MeetingMinutes;
    
        public ICollection<MeetingTopic> MeetingTopics { get; set; } = new HashSet<MeetingTopic>();

        [Display(Name = "Meeting Documents")]
        public ICollection<MeetingDocuments> MeetingDocuments { get; set; } = new   HashSet<MeetingDocuments>();

        [Display(Name = "Meeting Notes")]
        [StringLength(100, ErrorMessage = "100 characters limit.")]
        public string MeetingNotes { get; set; }
    }

SQL table create script being created from the migration which is missing all the field except for MeetingNotes

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  migrationBuilder.CreateTable(
                name: "Meeting",
                columns: table => new
                {
                    ID = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    MeetingNotes = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Meeting", x => x.ID);
                });

So far I’ve tried deleting all migrations and even the database and rebuilding but the new migration still return the same table with missing fields.

If anyone has any idea of what could be causing it would be very helpful. Thanks

>Solution :

It seems that the issue is with the way your code is defining the class properties. In the class, some of the properties are declared as fields (e.g. public string MeetingTopic;) instead of properties (e.g. public string MeetingTopic { get; set; };). Properties are used to manage access to an object’s data, while fields are just variables declared within a class. Fields don’t participate in property change notifications and are not automatically included in the Entity Framework migrations.

To resolve the issue, change all fields to properties and then re-run the migrations. This should generate the correct SQL table create script.

corrected code:

public class Meeting
{
    public int ID { get; set; }

    [Display(Name = "Meeting Topic")]
    public string MeetingTopic { get; set; }   

    [Display(Name = "Meeting Minutes")]
    public int MeetingMinutes { get; set; }
    
    public ICollection<MeetingTopic> MeetingTopics { get; set; } = new HashSet<MeetingTopic>();

    [Display(Name = "Meeting Documents")]
    public ICollection<MeetingDocuments> MeetingDocuments { get; set; } = new   HashSet<MeetingDocuments>();

    [Display(Name = "Meeting Notes")]
    [StringLength(100, ErrorMessage = "100 characters limit.")]
    public string MeetingNotes { get; set; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading