0

I have created a new project MVC 3.0 in VS 2010. By default the database is SQL Server Express.

I created a basic model:

public class Movie
{
    public int ID { get; set; }
    public String Movie { get; set; }
}

public class MovieDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }
}

Now I've setup the MVC project with mysql following this answer

In the database (MySQL) some tables were created automatically (my_aspnet_applications, my_aspnet_membership, my_aspnet_profiles, my_aspnet_roles, my_aspnet_schemaversion, my_aspnet_sessioncleanup, my_aspnet_sessions, my_aspnet_users, my_aspnet_usersinroles).

But my table Movie is not created. It's still in SQL Server Express. But I don't understand why is this possible, my web.config only has MySQL configuration:

<configuration>
  <connectionStrings>
    <add name="MySqlMembershipConnection"
          connectionString="Data Source=[myserver];
                            userid=[myuser];
                            password=[mypassword];
                            database=Movies;"
          providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>

  <appSettings>
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>

    <membership defaultProvider="MySqlMembershipProvider">
      <providers>
        <clear/>
        <add name="MySqlMembershipProvider"
             type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, 
                 Version=6.6.4.0, Culture=neutral, 
                 PublicKeyToken=c5687fc88969c44d"
             connectionStringName="MySqlMembershipConnection"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             requiresUniqueEmail="true"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="6"
             minRequiredNonalphanumericCharacters="0"
             passwordAttemptWindow="10"
             applicationName="/"
             autogenerateschema="true"/>
      </providers>
    </membership>

    <authentication mode="Windows" />

    <authorization>
      <deny users="?" />
    </authorization>

    <profile>
      <providers>
        <clear/>
        <add type="MySql.Web.Security.MySQLProfileProvider, MySql.Web, 
                 Version=6.6.4.0, Culture=neutral, 
                 PublicKeyToken=c5687fc88969c44d"
             name="MySqlProfileProvider"
             applicationName="/"
             connectionStringName="MySqlMembershipConnection"
             autogenerateschema="true"/>
      </providers>
    </profile>

    <roleManager enabled="true" defaultProvider="MySqlRoleProvider">
      <providers>
        <clear />
        <add connectionStringName="MySqlMembershipConnection"
             applicationName="/"
             name="MySqlRoleProvider"
             type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, 
                 Version=6.6.4.0, Culture=neutral, 
                 PublicKeyToken=c5687fc88969c44d"
             autogenerateschema="true"/>
      </providers>
    </roleManager>

    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages"/>
      </namespaces>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Thank you very much.

Community
  • 1
  • 1
Alberto
  • 704
  • 2
  • 12
  • 26

2 Answers2

1

Add a connection string named MovieDB that points to your MySql database. Or use the DbContext constructor that accepts a connection string name, passing MySqlMembershipConnection.

jrummell
  • 42,637
  • 17
  • 112
  • 171
  • Thank you very much. I created a new class inside MovieDB: public class MovieDBContext : DbContext { public MovieDBContext() : base("MySqlMembershipConnection") { } public DbSet Movies { get; set; } } And it worked perfect, although I have to delete maually the previous database in MySQL. By the way, why the others tables (my_aspnet_applications, my_aspnet_membership, ...) don´t exist now? Or better, are they usefull for anything? – Alberto Dec 19 '12 at 10:02
  • Are they in the same database as Movies? By default EF will drop and recreate your database whenever your context or entities change. You may want to use separate databases or [set the context's database initializer to null](http://msdn.microsoft.com/en-us/library/gg679461(v=vs.103).aspx). – jrummell Dec 19 '12 at 13:20
0

Try renaming your connection string from "MySqlMembershipConnection" to "MovieDB". Make sure to update all your membership pieces to reflect the new name.

This answer has some good information in it: https://stackoverflow.com/a/4924678/139694

Community
  • 1
  • 1
Sam
  • 9,933
  • 12
  • 68
  • 104