ASP.NET MVC 4 EF5 with MySQL
所以我刚拿起VS2012,我想用EF5启动ASP.NET MVC 4应用程序。
我的主机没有MSSQL,所以我必须使用MySQL。
如何告诉我的应用程序应使用MySQL? (我想使用devart MySQL连接器,也可以使用mysql.com的连接器)
您需要使用连接字符串DbProviderFactory和用于MySql Connector 6.5.4的自定义DatabaseInitializer来设置配置。我已经详细介绍了让EF5和MySql发挥作用的完整步骤,包括博客上初始化程序的代码。如果您需要ASP.Net成员资格提供程序解决方案,请先询问以下内容:MySQL的ASP.NET成员资格/角色提供程序?我还将在此处发布解决方案,以获取完整的EF5 MySql解决方案。
MySql连接器当前不支持EF 5迁移,并且ASP.NET仅在MS SQL上支持SimpleMembership(默认为MVC4),而不支持MySql。以下解决方案适用于Code First。
这些步骤是:
DbProvider
1 2 3 4 5 6 7 8 | <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> |
连接字符串
1 2 3 4 5 | <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/
然后将此添加到配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <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成员资格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <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和View运行:
1 |
Install Package:
1 2 3 | PM> Install-Package EntityFramework PM> Update-Package EntityFramework PM> Install-Package MySql.Data.Entity |
Web.config
1 2 3 4 5 | <connectionStrings> <add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost;port=3306;Initial Catalog=api_db;User Id=root;password=''"/> </connectionStrings> |
Create Model Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Web; namespace LiteRemit.Models { [Table("customers")] public class CustomerModel { [Key] public int CustomerId { get; set; } public string Name { get; set; } public string Country { get; set; } } } |
Create Model Context:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace LiteRemit.Models { public class MySqlCon : DbContext { //MySql Database connection String public MySqlCon() : base(nameOrConnectionString:"DefaultConnection") { } public virtual DbSet<CustomerModel> Customers { get; set; } } } |
Create Controller Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using LiteRemit.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace LiteRemit.Controllers { public class HomeController : Controller { MySqlCon _con; public HomeController() { _con = new MySqlCon(); } public ActionResult Index() { return View(_con.Customers.ToList()); } } } |
Code add view page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |