Pratik
Pratik

Reputation: 888

Entity framework : Invalid column name

I am getting this strange error from Entity framework , I cant figure out what I am doing wrong ..

My DB context code.

  public DbSet<Interaction> Interactions { get; set; }

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            InteractionsDBContextConfig(modelBuilder);

            base.OnModelCreating(modelBuilder);
        }

        public static void InteractionsDBContextConfig(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            // Interaction entity configuration.
            modelBuilder.Entity<Interaction>()
                .HasKey<int>(key => key.InteractionId);


            modelBuilder.Entity<Interaction>()
                .HasRequired<Form>(x => x.Form)
                .WithMany()
                .HasForeignKey(y => y.FormId);


            modelBuilder.Entity<Interaction>()
               .HasRequired<User>(x => x.User)
               .WithMany()
               .HasForeignKey(y => y.InteractionUserId);


            // User entity configuration.

            modelBuilder.Entity<User>()
                .HasKey<int>(x => x.UserId);

            modelBuilder.Entity<User>()
                .Property(x => x.UserName)
                .IsRequired()
                .HasColumnAnnotation(
                    IndexAnnotation.AnnotationName,
                    new IndexAnnotation(
                        new System.ComponentModel.DataAnnotations.Schema.IndexAttribute() { IsUnique = true }
                        )
                );
        }

My Entity

public class Interaction
    {
        public int InteractionId { get; set; }
        public int FormId { get; set; }
        public string InteractionName { get; set; }
        public virtual Form Form { get; set; }
        public int InteractionUserId { get; set; }

        public virtual User User { get; set; }
        public DateTime Deadline { get; set; }
        public int InteractionType { get; set; }
    }

when I do the following

var db = dbcontext.Interactions.ToList();

I get the following error

{"Invalid column name 'InteractionType'."}

I cant figure out why the error is showing up , why cant Entity framework see my column ? Changing the name of the column doesnt seem to help.

Upvotes: 3

Views: 6224

Answers (3)

Mark
Mark

Reputation: 419

I do not really have a clue what exactly went wrong in my case. I did upgraded the EF tools and I start getting the messages invalid column name. I solved it by removing all seed code in the OnModelCreating and then I also needed to remove this in Up method of the migration. Then I got a clean Migration script without the errors and the DB update was clean without errors. Now lets see if the next migration is clean from the start...

Upvotes: 0

Pratik
Pratik

Reputation: 888

I could not find the reason for the error getting shown.

I ended up resetting all my migrations by using the method mentioned here : https://stackoverflow.com/a/22451879/2794980

I guess the Migration history table and the migrations .CS files had some kind of mismatch.

Resetting everything solved my issue.

Upvotes: 0

Renan Silveira
Renan Silveira

Reputation: 146


Probably you changed the Interactions class adding "InteractionType" property and it wasn't reflected on Database and you need to use Migrations.

Open the menu on Visual Studio View> Others Windows> Package Manager Console, after that on Package Manager Console write "Enable-Migrations" and press Enter.

Then will be generated a folder named "Migrations" in your project, into that folder contains Configuration class and there you need to set AutomaticMigrationsEnabled property to "true" like below.

  public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }


You come back to Package Manager Console and write the command "Update-Database" and your project will work again.

Upvotes: 4

Related Questions