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配置文件
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <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, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data> <entityFramework > <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6"/> <providers> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" /> </providers> </entityFramework>
<connectionStrings> <add name="MyContext" connectionString="Data Source=120.77.46.150; Initial Catalog=beikeda_da;user id=keedb;passWord=1234;" providerName="MySql.Data.MySqlClient"/> </connectionStrings></configuration>
4 修改 myDbContext类文件,增加属性 [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
5 数据库迁移:参考
Database.SetInitializer(new MigrateDatabaseToLatestVersion<makerdb, Configuration<makerdb>>()); Database.SetInitializer(new CreateDatabaseIfNotExists<makerdb>()); Database.SetInitializer<makerdb>(null);
internal sealed class Configuration<TContext> : DbMigrationsConfiguration<TContext> where TContext : DbContext { public Configuration() { AutomaticMigrationsEnabled = true ; AutomaticMigrationDataLossAllowed = true; // register mysql code generator SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
} }
新闻热点
疑难解答