1.更新程序包EntityFramework->6.1.3
2.下载程序包MySql.Data.Entities(Ver:6.8.3)
3.修改config.cs或者web.config,包含以下内容
<configuration>
<configSections>
<connectionStrings>
<add name="MyDb" connectionString="Data Source=数据库服务器地址;Initial Catalog=数据库名称;user id=root;password=密码;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
4.创建类
public class AppLog
{
public long AppLogId { get; set; }
public DateTime Dt{get;set;}
public int Level { get; set; }
public string Source { get; set; }
public string MessageText { get; set; }
}
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class AppDbContext : DbContext
{
public DbSet<AppLog> AppLogs { get; set; }
public AppDbContext()
: base("name=MyDb")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
5.生成数据库/绑定数据库
(1)在程序包管理器控制台输入:Enable-Migrations 或者带参数 -force
(2)修改自动生成的类Configuration
AutomaticMigrationsEnabled = true;
6.更新数据库
在程序包管理器控制台输入:update-database 或者带参数-force