c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库

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

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第1张图片

2.添加一下引用 :

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

2.2  Microsoft.EntityFrameworkCore

2.3 Microsoft.EntityFrameworkCore.Design

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第2张图片

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

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

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第3张图片

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

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第4张图片

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

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

using CodeFirst.Model.Entity;

using Microsoft.EntityFrameworkCore;

using System;

using System.Collections.Generic;

using System.Text;

namespace CodeFirst.Model.Db

{

public class StudentDbContext: DbContext

{

public StudentDbContext(DbContextOptions options)

: base(options)

{

}

protected override void OnConfiguring(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; }

}

}

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.Text;

namespace CodeFirst.Model.Entity

{

public class Student

{

[Key]

public int Id { get; set; }

public string Name { get; set; }

public Teacher Teach { get; set; }

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace CodeFirst.Model.Entity

{

public class Teacher

{

public int Id { get; set; }

public int Age { get; set; }

public List Stus { get; set; }

}

}

7. 在Startup将StudentDbContext 注册为服务

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第5张图片

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

using CodeFirst.Model.Db;

using Microsoft.EntityFrameworkCore;

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading.Tasks;

namespace CodeFirst.Model

{

public class DbInitializer

{

public void InitializeAsync(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的调用。

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第6张图片

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

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

Build succeeded.

Warning(s)

Error(s)

Time Elapsed ::02.32

Done. To undo this action, use 'ef migrations remove'

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

4d44484a86dbe07b0f4763ca4c1e7d0d.png

解决办法执行下面

定位到csproject

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

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第7张图片

完了继续操作就行

PM> add-migration test

PM> update-database

生成数据库表:

ee95d4d2ef526d8c932db6747dd36bcf.png

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

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

public string Sex { get; set; }

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

a7fdbf003130d6babc1112c6d2d049f6.png

Add-Migration ModifyStudent

8d8e2ff6063d6d043e37ea7897e6d91d.png

再执行

PM> Update-Database

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第8张图片

数据库已经更新

c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库_第9张图片

Demo源码: Github

C# 通过自定义特性 实现根据实体类自动创建数据库表

.Net新手通常容易把属性(Property)跟特性(Attribute)搞混,其实这是两种不同的东西 属性指的类中封装的数据字段:而特性是对类.字段.方法和属性等元素标注的声明性信息 如下代码(Id ...

学习MVC之租房网站(三)-编写实体类并创建数据库

在上一篇中,搭建好了项目框架,并配置了EF.Log4Net和进程外Session.接下来会编写Eneity类并采用CodeFirst的方式 ...

Do You Kown Asp.Net Core - 根据实体类自动创建Razor Page CURD页面模板

Scaffolding Template Intro 我们知道在Asp.Net MVC中,如果你使用的EF的DBContext的话,你可以在vs中通过右键解决方案-添加控制器-添加包含视图的控制器,然 ...

Hibrenate实现根据实体类自动创建表或添加字段

Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 实现: 在配置hibernate的配置文件中将hbm2ddl.auto设置为update,如:Xml代码&l ...

Hibernate根据实体类自动创建表

Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码

IntelliJ IDEA 2017版 spring-boot 实现jpa基本部署,通过实体类自动建立数据库

一.添加Spring Boot JPA-Hibernate步骤 1.在pom.xml添加mysql,spring-data-jpa依赖      2.在application.properties文件 ...

EF自动创建数据库步骤之一(实体类写法)

文章演示使用EF自动创建数据库第一个步骤创建实体类. 一.创建表映射实体类 using System; using System.Collections.Generic; using System.C ...

SpringBoot使用Hibernate,实现自动创建数据库表【博客数据库设计】

我们准备设计博客,那就要设计数据库. 我们可以使用Hibernate来自动生成数据库. 博客数据库的结构: 实体类: 博客 Blog 博客分类 Type 博客标签 Tag 博客评论 Comment 用 ...

企业项目实战 .Net Core + Vue/Angular 分库分表日志系统五 | 完善业务自动创建数据库

教程预览 01 | 前言 02 | 简单的分库分表设计 03 | 控制反转搭配简单业务 04 | 强化设计方案 05 | 完善业务自动创建数据库 说明 这节来把基础的业务部分完善一下. 因为 IQue ...

随机推荐

java swing 双人五子棋源代码

import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...

Javascript-9-1-OOP-5-链式调用

Ubuntu上安装Apache、MySql和PHP

参考文献: http://developer.51cto.com/art/201110/299303.htm

HTML中的边框属性

可以通过边框风格属性border-style设定上下左右边框的风格,该属性用于设置一个元素边框的样式,而且必须用于指定可见的边框.可以使用1到4个关键字,如果四个值都给出了,它们分别用于上.右.下和左 ...

基于HTML5的捕鱼达人游戏网页版

之前给大家分享了html5实现的水果忍者,愤怒的小鸟,中国象棋游戏.今天给大家分享一款捕鱼达人(fishjoy)网页版游戏的源码.可以在线玩也可以下载到本地.它使用html5技术和javascript ...

Ralink RT3290无线网卡驱动安装 (linux)

Ralink RT3290无线网卡驱动安装 (linux, 笔记备忘) 1. 设备信息查看无线网卡设备信息 # lspci : 2. 驱动下载http://pan.baidu.com/s/1sjsHN ...

Agri Net POJ1258 &;&; Constructing Roads POJ2421

题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include #include #include

利用autoit自动关闭指定标题窗口

 最近使用PL/SQL Developer 比较两个数据库数据差异,因部分表上没有主键,PL/SQL 就会弹出一个确认框提示某某表没有主键.因为有很多表没有主键,就不停的弹出确认窗口,得不停的点击 ...

Objective-C 使用核心动画CAAnimation实现动画

先来看看效果吧 整个核心动画就不多做介绍了,随便一搜就能有很多很详细的解释,主要使用以下四种 CABasicAnimation //经典动画 CAKeyframeAnimation //关键帧动画 C ...

Sublime 个人配置

Sublime 个人配置 用的faltland主题,之后还加了一些自己喜欢的东西. 效果图如下: { "always_show_minimap_viewport": true, & ...

你可能感兴趣的:(c,自动生成mysql表结构)