简体   繁体   中英

System.InvalidOperationException >> occurred in EntityFramework.dll but was not handled in user code?

I would like to ask about this problem please. this my code>

context :

public class ArchifeContext: DbContext
{
    public DbSet<Department> Departments { get; set; }
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Book> Books { get; set; }
}

Controller :

public ArchifeContext db = new ArchifeContext();

public ActionResult Index()
{
    return View(db.Books.ToList());
}

This might be occurring because your database doesn't match your context, try to backup and delete old database and try again. If you don't wana do this then try to migrate changes to your database by using Enable-Migrations and Update-Database

Try this.It worked for me

  1. Enable-Migrations
  2. Add-Migration (Migration Name)
  3. update-database
  4. Run Again

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