0

我在 VS 2010 中创建了一个新项目 MVC 3.0。默认情况下,数据库是 SQL Server Express。

我创建了一个基本模型:

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

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

现在我已经按照这个答案用mysql设置了MVC项目

在数据库 (MySQL) 中,自动创建了一些表(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)。

但是我的表Movie没有创建。它仍在 SQL Server Express 中。但我不明白为什么会这样,我web.config只有 MySQL 配置:

<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>

非常感谢你。

4

2 回答 2

1

添加一个名为MovieDB指向您的 MySql 数据库的连接字符串。或者使用DbContext接受连接字符串名称的构造函数,传递MySqlMembershipConnection.

于 2012-12-18T13:56:30.023 回答
0

尝试将连接字符串从“MySqlMembershipConnection”重命名为“MovieDB”。确保更新您的所有会员资料以反映新名称。

这个答案中有一些很好的信息:https ://stackoverflow.com/a/4924678/139694

于 2012-12-18T13:57:29.507 回答