Dbupdateexception exception

Asked

Viewed 672 times

3

I am trying to study EF with Code First. I am getting this exception on 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 follow 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 (http://go.microsoft.com/fwlink/? Linkid=238269).

  • I’m managing to create the database, but I’m not able to insert data in it.

  • Your question already has an answer here, but I marked the wrong duplicate. See if it fits you.

  • Funny! I was studying here : [http://www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx]. I commented some Properties class Student and spun!

  • @Ciganomorrisonmendez It’s a very interesting approach!! I loved your answer. But I have another problem, when I try to create another class I get in context ctx.Professores.Add(prof) I receive an exception 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 ([http://go.microsoft.com/fwlink/? Linkid=238269]).

  • Edit your answer by placing this new error (do not delete the previous text) that I answer to you.

1 answer

3


For this mistake:

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 (http://go.microsoft.com/fwlink/? Linkid=238269).

You mapped the Models but did not generate incremental bank variation, 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 in the database. Once done, type now:

PM> Update-Database

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


If this error appears:

No Migrations Configuration type was found in the Assembly 'School'. (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 set up your project, then repeat the steps to generate your Migration again:

PM> Add-Migration Inicial
PM> Update-Database
  • Thanks again for the great explanation. I’m really learning a lot! But I’m still having mistakes... When I type Add-Migration NomeDaMigration I get this "No Migrations Configuration type was found in the Assembly 'School'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a Migrations Configuration)".

  • Ah, yes. I will update the answer.

  • @tek6 See now.

  • Millions of Thanks to you! Solved my problem and taught me the basics of EF Code First! @ciganomorrisonmendez

Browser other questions tagged

You are not signed in. Login or sign up in order to post.