c 创建mysql实体模型_EntityFrameworkCore 根据实体类自动创建数据库

本文介绍了如何在C#的Asp.Net Core WebApi项目中,利用EntityFrameworkCore和Pomelo.EntityFrameworkCore.MySql库实现MySQL数据库的Code First创建。详细步骤包括添加必要的引用、配置项目、创建DbContext、定义实体类、注册服务、使用Migrations初始化数据库,以及如何通过命令行工具处理数据库迁移。
摘要由CSDN通过智能技术生成

1.首先新建 Asp.Net Core WebApi 项目

1c0cef393c40ec106a8fa0dd9fbef89b.png

2.添加一下引用 :

2.1   Pomelo.EntityFrameworkCore.MySql(我用的Mysql 根据自己情况引用就行)

2.2  Microsoft.EntityFrameworkCore

2.3 Microsoft.EntityFrameworkCore.Design

6ea5edbadcba7929198a7c75e8c66d64.png

3.使项目支持dotnet ef 工具以使用Migrations

3.1 手动修改csproj文件(手动添加是因为在nuget添加Microsoft.EntityFrameworkCore.Tools.DotNet 时报错,估计是vs的问题),添加一下配置

a7e6b3da2bd0c716e241019a28395dba.png

4.打开CMD命令 cd到项目目录下(C:\Users\Administrator\source\repos\CodeFirst\CodeFirst),执行

dotnet build

Microsoft (R) Build Engine version 15.1.545.13942 Copyright (C) Microsoft Corporation. All rights reserved. Startup.cs(45,13): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo.csproj] EntityFrameworkCoreMigrationsDemo -> C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\bin\Debug\netcoreapp1.0\EntityFrameworkCoreMigrationsDemo.dll Build succeeded. Startup.cs(45,13): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo.csproj] 1 Warning(s) 0 Error(s) Time Elapsed 00:00:04.76

5. 第4步完成后继续执行

dotnet ef

6cb81552cd4ae77d4e480a9d0a4fd8c6.png

可以看见独角兽就说明引用成功了。距离胜利更近一步了

6.创建实例 StudentDbContext和相关实体类

usingCodeFirst.Model.Entity;usingMicrosoft.EntityFrameworkCore;usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceCodeFirst.Model.Db

{public classStudentDbContext: DbContext

{public StudentDbContext(DbContextOptionsoptions)

:base(options)

{

}protected override voidOnConfiguring(DbContextOptionsBuilder optionsBuilder)

{

optionsBuilder.UseMySql(@"Server=localhost;Port=3306;Database=Policy;UId=root;Pwd=mysql.com");

}public DbSet Student { get; set; }public DbSet Teacher { get; set; }

}

}

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Text;namespaceCodeFirst.Model.Entity

{public classStudent

{

[Key]public int Id { get; set; }public string Name { get; set; }public Teacher Teach { get; set; }

}

}

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceCodeFirst.Model.Entity

{public classTeacher

{public int Id { get; set; }public int Age { get; set; }public List Stus { get; set; }

}

}

7. 在Startup将StudentDbContext 注册为服务

6d20ac6da0a05b3ff0d76a9555c73490.png

8.使用Migrations  新建数据库初始化类DbInitializer

usingCodeFirst.Model.Db;usingMicrosoft.EntityFrameworkCore;usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Threading.Tasks;namespaceCodeFirst.Model

{public classDbInitializer

{public voidInitializeAsync(StudentDbContext context)

{//var migrations = await context.Database.GetPendingMigrationsAsync();//获取未应用的Migrations,不必要,MigrateAsync方法会自动处理

context.Database.MigrateAsync();//根据Migrations修改/创建数据库

}

}

}

9.在Startup.cs的Configure方法中,添加参数StudentContext context,netcore会使用DI将DbContext注入到Configure方法,并添加对DbInitializer的调用。

affd1f854391667023653fcd00fe30a1.png

10.使用dotnet ef命令创建Migrations:dotnet ef migrations add text  text随便起名

C:\Users\Administrator\source\repos\CodeFirst\CodeFirst>dotnet ef migrations add Initial

Build succeeded.0Warning(s)0Error(s)

Time Elapsed00:00:02.32Done. To undothis action, use 'ef migrations remove'

如果执行dotnet ef migrations add Initial报错 :

b93d2ba5f8579c2e00125c63da026039.png

解决办法执行下面

定位到csproject

PM> dotnet ef migrations script --verbose -i --project "C:\Users\Administrator\source\repos\CodeFirst\CodeFirst"

eb2d2b02cb312de6407083344d5f7212.png

完了继续操作就行

PM>add-migration test

PM>update-database

生成数据库表:

804f53b39f7be6573b3ce9162c43031b.png

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

修改Student实体类加一个Sex字段

public string Sex { get; set; }

执行 (注意执行命令之前在控制台‘默认项目’列表选中DbContext所在的类库,不然爆错)

0102a877443a8994db1aea430921d654.png

Add-Migration ModifyStudent

6e05fd942dab9a8f1a70770531fd16fc.png

再执行

PM> Update-Database

48d15345103778130d13c6e1d9436cdd.png

数据库已经更新

7c002b1de633d22662d9c6a474508735.png

Demo源码: Github

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值