当前位置:Gxlcms > 数据库问题 > Mysql EF Code First

Mysql EF Code First

时间:2021-07-01 10:21:17 帮助过:2人阅读

<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

 

Mysql EF Code First

标签:

人气教程排行