Search code examples
mysqlasp.net-mvcef-code-first

Problem with EF6-code first and Mysql in ASP.NET MVC5


I want to create a simple code-first ASP.NET MVC5 project using EF6 and Mysql, but after adding suitable Nupkg to my project and editing web.config and then scaffolding controller,views,..., the created database has two problems as bellow: enter image description here

Problems: no _migrationhistory table created and the PK column is not assigned ! and this is my web.config:

 <connectionStrings>
    <remove name="LocalMySqlServer" />
    <add name="LocalMySqlServer" connectionString="" providerName="MySql.Data.MySqlClient" />
    <add name="TestContext" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost;userid=root;password=***;database=Test_DB;Integrated Security=True;CharSet=utf8 ;" />
  </connectionStrings>

  <entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>

After creating my controller,views, ... automatically (scaffolding) and running the project, everything looks well but in database things are different.

In a new project,after scaffolding(creating automatic controller,views,context,...) I didn't run the program !! and instead I did : enable-migrations, add-migration createDB,update-database , and the result was creating tables well and __migrationhistory was created too, but after that when I run the project, it says that :

The model backing the 'TestContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?>LinkId=238269)

It's good to say that in a windows form application,using above migration codes, I had no problem using code first to create data base, tables and run the program. But in ASP.NET MVC5 after using the above codes for migration, the project doesn't run as you see the error above


Solution

  • Solved- I tried more than 20 times and I understood that before running the program, I should enable-migration/add migration/update-database- and I don't know what happened but I do not see this error after running project any more.

    The model backing the 'TestContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)