解决方法
MysqL连接器当前不支持EF 5迁移,ASP.NET仅支持MS sql不是MysqL的SimpleMembership(MVC4默认值)。以下解决方案适用于Code First。
步骤是:
从NuGet抓住EF 5
>从NuGet(6.5.4)或MysqL(6.6.4)中抓取MysqL.Data和MysqL.Data.Entity
>配置MysqL数据提供程序
>配置MysqL连接字符串
>创建自定义MysqL数据库初始化程序
>配置自定义MysqL数据库初始化程序
>如果需要,请配置ASP.NET成员资格
DbProvider
<system.data> <DbProviderFactories> <remove 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" /> </DbProviderFactories> </system.data>
连接字符串
<connectionStrings> <add name="ConnectionStringName" connectionString="Datasource=hostname;Database=schema_name;uid=username;pwd=Pa$$w0rd;" providerName="MysqL.Data.MysqLClient" /> </connectionStrings>
数据库初始化程序
如果您使用NuGet(6.5.4)中的MysqL连接器,则需要自定义初始化程序。代码可在http://brice-lambson.blogspot.se/2012/05/using-entity-framework-code-first-with.html
或http://www.nsilverbullet.net/2012/11/07/6-steps-to-get-entity-framework-5-working-with-mysql-5-5/
然后将其添加到配置中
<configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version=5.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" /> </configSections> <entityFramework> <contexts> <context type="Namespace.YourContextName,AssemblyName"> <databaseInitializer type="Namespace.YourChosenInitializer,AssemblyName"> </databaseInitializer> </context> </contexts> <defaultConnectionFactory type="MysqL.Data.MysqLClient.MysqLClientFactory,MysqL.Data" /> </entityFramework>
ASP.NET成员资格
<membership defaultProvider="MysqLMembershipProvider"> <providers> <clear /> <add name="MysqLMembershipProvider" type="MysqL.Web.Security.MysqLMembershipProvider,MysqL.Web,Version=6.5.4.0,PublicKeyToken=c5687fc88969c44d" autogenerateschema="true" connectionStringName="*NAME_OF_YOUR_CONN_STRING*" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minrequiredPasswordLength="6" minrequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" /> </providers> </membership>
获取AccountController和Views工作:
>删除MVC 4 AccountController,AccountModels,帐户视图文件夹和_LoginPartial共享视图>创建一个新的MVC 3 Web应用程序>将MVC 3 AccountController,AccountModels,帐户视图文件夹和_logonPartial共享视图复制到您的MVC 4应用程序中>在@ Html.Partial(“_ logonPartial”)的共享_Layout视图中替换@ Html.Partial(“_ LoginPartial”)