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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With