1 安装必要的开发包
MySQL For Visual Studio 和 Connector/NET
2 Nuget安装MySQL库: Install-Package MySql.Data.Entity -Version 6.9.8
安装完毕后,项目中的引用自动添加EntityFramework、MySql.Data、MySql.data.Entity.EF6 三个程序集
3 编辑app.config配置文件
4 修改 myDbContext类文件,增加属性 [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
5 数据库迁移:参考
Database.SetInitializer(new MigrateDatabaseToLatestVersion>());
Database.SetInitializer(new CreateDatabaseIfNotExists());
Database.SetInitializer(null);
internal sealed class Configuration : DbMigrationsConfiguration where TContext : DbContext
{
public Configuration()
{
AutomaticMigrationsEnabled = true ;
AutomaticMigrationDataLossAllowed = true;
// register mysql code generator
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
}
}