Create a new project named MySqlTest
-
Install following packages by right-clicking on the References folder of the project and selecting Manage NuGet Packages...
- EntityFramework
- MySql.Data
- MySql.data.Entity
Update the app.config file
-
Add a model and the DbContext
public class User
{
public int UserId { get; set; } public string Name { get; set; }
} public class MyDb:DbContext
{
public MyDb():base("name=TestDb")
{ } public DbSet<User> Users { get; set; }
} -
Add some Test Codes
static void Main(string[] args)
{
using (MyDb db = new MyDb())
{
User u = new User { Name = "Joey"};
db.Users.Add(u);
db.SaveChanges();
} Console.ReadLine();
} Compile the project
Enable migrations
Run the Enable-Migrations command in Package Manager ConsoleAdd the first Migration
Run the Add-Migration init command in Package Manager ConsoleUpdate Database
Run the Update-Database command in Package Manager ConsoleTo check whether the table named users is created
Run the Projcet and check is there any data have been inserted
相关文章
- Entity Framework 6 Recipes 2nd Edition(10-5)译 -> 在存储模型中使用自定义函数
- Entity Framework 6 Recipes 2nd Edition(11-9)译 -> 在LINQ中使用规范函数
- 创建Entity Framework数据库时出现Mysql语法错误
- 填坑 - 使用Entity Framework 6 + Sqlite进行DB first开发
- Entity Framework 6 Recipes 2nd Edition(9-1)译->用Web Api更新单独分离的实体
- 让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
- 《Entity Framework 6 Recipes》中文翻译系列 (34) ------ 第六章 继承与建模高级应用之多条件与QueryView
- 《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集
- Entity Framework 6 Recipes 2nd Edition(10-3)译 -> 返回结果是一个标量值
- 《Entity Framework 6 Recipes》翻译系列 (4) -----第二章 实体数据建模基础之从已存在的数据库创建模型