1

I'm developing a project and I made changes to my models, and now whenever I run my project I get these errors for all the model that I made changes to:

for my person model:

Invalid column name 'Location'.

Invalid column name 'EmailAddress'.

Invalid column name 'PlaceOfBirth'.

for my guardian model:

Invalid object name 'Admission.Guardian'.

Here is my DbContext model:

public class SchoolInfoEntities: DbContext
    {
        public DbSet<Students> Student { get; set; }
        public DbSet<Class> Classes { get; set; }
        public DbSet<Guardian> Guardians { get; set; }
        public DbSet<Staff> Staffs { get; set; }
        public DbSet<Subject> Subjects { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<SchoolDetails> SchoolDetails { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Students>()
                .HasMany(t => t.Guardians)
                .WithMany(t => t.Students)
                .Map(m =>
               {
                   m.ToTable("Admission.StudentGuardian");
                   m.MapLeftKey("StudentId");
                   m.MapRightKey("GuardianId");
               });

            modelBuilder.Entity<Staff>()
                .HasMany(t => t.Subjects)
                .WithMany(t => t.Staffs)
                .Map(m =>
                {
                    m.ToTable("Admission.SubjectInstructor");
                    m.MapLeftKey("StaffId");
                    m.MapRightKey("SubjectName");
                });

            modelBuilder.Entity<Staff>()
                .HasMany(t => t.Departments)
                .WithMany(t => t.Staffs)
                .Map(m =>
                {
                    m.ToTable("Admission.StaffDepartment");
                    m.MapLeftKey("StaffId");
                    m.MapRightKey("DepartmentId");
                });

            Database.SetInitializer<SchoolInfoEntities>(null);

        }
    }

Is there anything that I'm not considering? Please help me guys.

Community
  • 1
  • 1
Nathan Siafa
  • 731
  • 4
  • 19
  • 39

3 Answers3

2

You can't change the model fields without updating the related database fields. You need to create a migration and apply it in order to keep your models in sync with the database schema.

https://docs.asp.net/en/latest/data/ef-mvc/migrations.html

If you have a production version of the database you'll need to run the same migrations on it.

Soviut
  • 88,194
  • 49
  • 192
  • 260
1

You need to run below commands on the Package Manger console before run your app.

Note : you need to set correct connection string where the place (db server) you need to run the scripts.

PM> Add-Migration "Added_New_Properties"

PM> Update-Database
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • What is "Added new properties"? – Nathan Siafa Nov 09 '16 at 19:58
  • that is a migration script name.you can give any name for it. – Sampath Nov 10 '16 at 02:56
  • Sorry @Sampath for the late feedback. I got it working thanks to the support of all of you guys. Can you please take a look at this: http://stackoverflow.com/questions/40663363/asp-net-mvc-views-for-many-to-many-relationship?noredirect=1#comment68558862_40663363 – Nathan Siafa Nov 19 '16 at 16:44
  • Glad to hear that it is working :) hope you can accept the answer too.this one or any other answer which you prefer.here you can see how to do that : http://stackoverflow.com/tour – Sampath Nov 19 '16 at 21:11
1

Since you are using Code First, EntityFramework always make sure that the model is synchronized with the database, when the application starts, it compares the classes and its properties with the tables and columns in the database taking into consideration any changes you made using the Fluent API inside your Context.

When you add a new property into any class, you have to either add a corresponding column in the database table that maps to this class, or you can let EntityFramework do it for you by using Migration.

You have to enbale migration first using the Enable-Migrations command

After that, run the command Add-Migration [Migration Name] which will compare the model to the database and generate some code inside the Migrations folder to update the database, it should have an Up and Down methods.

To run the Up method which updates the database to the code, you have to run the Update-Database command.

All of these commands must run inside the Package Manager Console which you can reach from Tools -> NuGet Package Manager -> Package Manager Console.

Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19