Subalfa Subalfa - 1 month ago 22
C# Question

ASP.NET MVC5 doesn't create MySQL database except break at the initializer

I'm a beginner in ASP.NET MVC5. Using the following tutorial:
http://www.ryadel.com/en/asp-net-setup-mvc5-website-mysql-entity-framework-6-code-first-vs2013/

I'm using the following code:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class MyDBContext : DbContext
{
public MyDBContext()
: base("MyDBContextConnectionString")
{
Database.SetInitializer<MyDBContext>(new MyDBInitializer());
}

public DbSet<User> Users{get;set;}
public DbSet<History> Histories{get;set;}
public DbSet<Place> Places{get;set;}
public DbSet<Review> Reviews{get;set;}
public DbSet<Type> Types{get;set;}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}


The web.config:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
</providers>
</entityFramework>

<connectionStrings>
<add name="MyDBContextConnectionString" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;UserId=root;Password=******;database=KorulottemDB;CharSet=utf8;Persist Security Info=True" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>


Unfortunately doesn't create the database except I break at the initializer:

Database.SetInitializer<MyDBContext>(new MyDBInitializer());


and click on the "The function evaluation requires all threads to run".

enter image description here

The database appears on the server in this case but without tables.

Thanks for all help.

Answer

Since you're using Code First approach, i recommend you to use Migrations too. To enabel that you can run the following command in Package Manager Console:

Enable-Migration -ProjectName <YourDataLayer>

You should try to use Package Manager Console, in Visual Studio, and run the following command:

Update-Database -ProjectName <YourDataLayer>