Exception DbUpdateException

3

I'm trying to study EF with Code First. I'm getting this exception at SaveChanges()

An unhandled exception of type System.Data.Entity.Infrastructure. DbUpdateException occurred in EntityFramework.dll

Additional information: An error occurred while updating the entries. See the inner exception for details.

Another problem would be when I try to create another class to serve as a table, I get an exception called InvalidOperationException follows the stack trace

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

Additional information: The model backing the 'SchoolContext' context has changed since the database was created. Consider using Code First Migrations to update the database ( link ).

    
asked by anonymous 02.09.2015 / 03:26

1 answer

3

For this error:

  An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

     

Additional information: The model backing the 'SchoolContext' context has changed since the database was created. Consider using Code First Migrations to update the database ( link ).

You have mapped the Models but did not generate the incremental variation of the database, called Migration . To generate a Migration open the Package Manager Console (View> Other Windows > Package Manager Console) and type:

PM> Add-Migration NomeDaMigration

This will generate a file with the modifications that should be made to the database. After that, type now:

PM> Update-Database

This will update your database with the modifications made to your Models . That done, just run your application.

If this error appears:

  

No migrations configuration type was found in the 'School' assembly. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration)

In the Package Manager Console, type:

PM> Enable-Migrations

Wait for Visual Studio to configure your project, then repeat the steps to generate your Migration again:

PM> Add-Migration Inicial
PM> Update-Database
    
02.09.2015 / 04:27