简体   繁体   中英

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.


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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM